Article posted by Stevnetk
on 7/6/2012 | Views: 1660 | Category: C#
| Level: Advance | Points: 250
There are 2 possible approaches to use the html to pdf converter in a non .NET application, assuming you have a Windows server which are discussed in this article.
How can I use a HTML to PDF .NET Library from a C++ Native Windows Application? There could be 2 approaches
1. You can create a simple ASP.NET application with only one page which receives an URL in query string and converts that URL to PDF and returns the resulted PDF in HTTP response. From your application (Pearl, PHP etc) you can redirect to that ASP.NET page giving the URL you want to convert in query string. To create the application you can modify our sample for ASP.NET to produce the PDF in PageLoad even handler instead of converting to PDF when a button is pressed
2. If you can use COM in your application, this article explains how to use the .NET library in a C++ application:v
For example HiQPdf library is a .NET library and it cannot be referenced directly into a native Windows application.
You can register the HiQPdf classes for COM clients and produce a type library file by executing the following command in an Administrator command prompt:
regasm HiQPdf.dll /tlb:HiQPdf.tlb
The HiQPdf.tlb will be included in the C++ application code to offer the prototypes for HiQPdf classes and methods. The compiler will produce a C++ header file named HiQPdf.tlh from the TLB file, containing the HiQPdf library types and methods prototypes.
The HiQPdf.dll and HiQPdf.dep files must be copied near the application executable or otherwise you have to install the HiQPdf.dll in GAC to make it available for COM infrastructure.
Using the code
The C++ code of a simple console application which converts an URL and saves the resulted PDF document to a file on disk looks like below:
#import "HiQPdf.tlb" raw_interfaces_only
using namespace HiQPdf;
// create the HTML to PDF converter
// get a reference to the PDF document control object from converter
_PdfDocumentControl * pdfDocumentControl;
// set PDF page orientation
// set PDF page margins
// create the URL to convert and output PDF file name strings
BSTR urlToConvert = SysAllocString(L"http://www.google.com");
BSTR outPdfFile = SysAllocString(L"out.pdf");
// call the converter to convert the HTML document to a PDF file
// free the allocated strings
It is very simple to use a html to pdf .NET library from COM clients to get quality PDFs.
If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.
Found interesting? Add this to: