0

Is there a way to convert a HTM file to a PDF? Based on my understanding, HTML and HTM file extensions are the same. With that in mind, I tried the following code using Spire but my output was a blank PDF.

if (filelist[f].EndsWith(".htm"))
{
    PdfDocument doc = new PdfDocument();

    string filext = System.IO.Path.GetExtension(filelist[f]);
    string outputDocName = filelist[f].Replace(filext, ".pdf");

    doc.SaveToFile(outputDocName);
    doc.Close();
}

I have searched on Google but I couldn't find much on converting a HTML file to a PDF. I have even looked into Python using ImageMagick, but there is multiple steps, so will try that once I run out of options. Is iTextSharp a possibility? Do I need to do another conversion to the HTM file to another file type before turning that into a PDF or for what I am trying to do doesn't exists?

Cœur
  • 37,241
  • 25
  • 195
  • 267
LampPost
  • 856
  • 11
  • 30

1 Answers1

0

HTML (with file extension of .htm or .html) is in a sense a plain text file which needs parsing and rendering to produce "visual" output. So ImageMagick or similar tools will not work if they have no concept of rendering HTML.

If this is a one-off requirement - get a PDF printer driver (for example CUPS PDF) and just "print" the pages from your browser.

If this needs to be an automated process my personal suggestion would be phantomJS. But search and Google are your friends - Converting HTML to PDF using PHP?

Community
  • 1
  • 1