![]()
Article posted by
Stevnetk on 7/6/2012 | Views: 1660 | Category:
C# | Level: Advance |
Points: 250
If you found
plagiarised (copied) or inappropriate content,
please
let us know the original source along with your correct email id (to communicate) for further action.
Advertisements
Advertisements
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.
Introduction
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.
Description
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:
#include <iostream>
#import "HiQPdf.tlb" raw_interfaces_only
using
namespace HiQPdf;int
main(){
CoInitialize(0);
{
// create the HTML to PDF converter_HtmlToPdfPtr htmlToPdfConverter(__uuidof(HtmlToPdf));
// get a reference to the PDF document control object from converter_PdfDocumentControl * pdfDocumentControl;
htmlToPdfConverter->get_Document(&pdfDocumentControl);
// set PDF page orientationpdfDocumentControl->put_PageOrientation(PdfPageOrientation_Portrait);
// set PDF page margins_PdfMargins* pdfMargins;
pdfDocumentControl->get_Margins(&pdfMargins);
pdfMargins->put_Left(10);
pdfMargins->put_Right(10);
pdfMargins->put_Top(10);
pdfMargins->put_Bottom(10);
// create the URL to convert and output PDF file name stringsBSTR 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
htmlToPdfConverter->ConvertUrlToFile(urlToConvert, outPdfFile);
// free the allocated stringsSysFreeString(urlToConvert);
SysFreeString(outPdfFile);
}
CoUninitialize();
return 0;}
Conclusion
It is very simple to use a html to pdf .NET library from COM clients to get quality PDFs.
Reference
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: