How can I use a HTML to PDF .NET Library from a C++ Native Windows Application

Stevnetk
Posted by in C# category on for Advance level | Points: 250 | Views : 3884 red flag

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 orientation

pdfDocumentControl->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 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

htmlToPdfConverter->ConvertUrlToFile(urlToConvert, outPdfFile);

 

// free the allocated strings

SysFreeString(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

Page copy protected against web site content infringement by Copyscape

About the Author

Stevnetk
Full Name: stevnetk stevnetk
Member Level: Starter
Member Status: Member
Member Since: 3/13/2012 4:07:32 PM
Country: United States

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)