Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5954
  • Last Modified:

How to convert html to pdf using MFC?


  Hi all,
     I have a requirement in which I should convert html file to pdf file format, I am able to convert html file to doc, txt and rtf formats by using msword9.olb type library, but I want to know how to approach for converting htm file to pdf format, or any other document format to pdf format using vc++.

waiting for reply,
  hareesh.
0
jntu_hareesh
Asked:
jntu_hareesh
  • 2
2 Solutions
 
jhanceCommented:
This is a bit like asking how to perform brain surgery assuming you have a knife...

Whether you're using VC++ or MFC or anything else, the difficult part of this is going to be for you to understand the PDF file format, then to understand how to "render" HTML into that format, and finally to do that actual coding in VC++, MFC, or whatever.

Most PDF writers (like Adobe Acrobat or [what I like] FinePrint's PDF Factory) are implemented as PRINTER DRIVERS in Windows.  So you use the existing application's (in this case IE) printer interface and then just print-to-PDF.  So the only thing you need to implement in your code is taking the Windows DC (device context) from the printer interface and writing out PDF.

There are some resources to assist you:

1) The Windows DDK includes SAMPLE printer drivers so you can understand how to do that in general.
2) There is an open source PDF writer: http://sourceforge.net/projects/pdfcreator/
3) The PDF file format is documented: http://partners.adobe.com/asn/tech/pdf/specifications.jsp

Enjoy.  This project, while certainly NOT trivial, it doable and should be interesting.
0
 
Karl Heinz KremerCommented:
First you should find out if you really have to code this in yourself in your application, or if you can use something that already does this conversion. One option of the latter group would be HTMLDoc from EasySW (http://www.easysw.com/htmldoc/).

If you want to use Acrobat (and the full version of Acrobat is installed on your system), you can first print to a PostScript file and then use the Distiller API to convert the PostScript file to PDF. The Acrobat SDK (available on the partners.adobe.com web site) does contain all the information you need to automate distiller. You can of course use other PS to PDF converters like Ghostscript (www.ghostscript.org) or Jaws PDF Creator (www.jawspdf.com)

All these a solutions however have one drawback: If you use Acrobat's Create PDF From Web page, all the link in the HTML code are converted to PDF links, so you can still click on a link in Acrobat, and if it's a link to the same document, Acrobat will jump to the new location, or if it's an external link, Acrobat will ask you if you want to open the new page in Acrobat or your web browser.

Your question about "any other format to PDF" is also something you should use the Distiller API for: As long as you can print to Postscript, you can create PDF. This however requires that you can automate the application that can consume the "other format" (e.g. MS Word for .doc files) so that you can print to Postscript.

Otherwise you have to understand all these formats and do it element by element, which is also possible, but requires documentation about these formats. The .doc format for example is not documented by Microsoft, so you would have to reverse engineer the format first.

If you look at how Acrobat converts "other formats" to PDF, they use exactly this solution (convert to Postscript first and then call the Distiller).

0
 
Karl Heinz KremerCommented:
I provided at least part of the solution.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now