Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 676 |  Welcome, Guest!   Register  Login
Home > Articles > C# > How can I use a HTML to PDF .NET Library from a C++ Native Windows Application

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

Article posted by Stevnetk on 7/6/2012 | Views: 1660 | Category: C# | Level: Advance | Points: 250 red flag

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

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:7 year(s)
Home page:http://www.dotnetfunda.com
Member since:Tuesday, March 13, 2012
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

Hello Team, In this article we will see how we can generate custom Auto Numbers and use it in Application

In this post you will learn how to write custom namespaces and nested namespaces in C#.

This article will demonstrate as how to create and use Status Bar control in Windows Application

In this article we will be exploring the RDLC reporting feature of .NET.In VS 2010 Crystal Reports are not supported and to create Reporting Systems we can use rdlc feature.

WeakReference comes very handy when working with Large objects. In this application, I have demonstrated how you could easily use this class to improve the performance of your application.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 12:17:52 PM