Convert HTML to PDF in C#, VB, and Java

You might have heard about how LEAD has a world-class Document Converter. Our Document Converter can easily transform HTML to PDF. The portability of a PDF allows you to access content from webpages across multiple devices both on- and off-line.

Below are a few code snippets to convert HTML to PDF.

Convert HTML to PDF in C#

First, initialize the Document Converter.

namespace Convert_Files_with_Document_Converter 
{ 
  class Program 
  { 
    static void Main(string[] args) 
    { 
      string directory = @"C:\InputFileDirectory"; 
      string input_file = Directory.GetFiles(directory, "example.html"); 
      string output_file = Path.Combine(directory, $"example.pdf"); 

      SetLicense(); //Sets the LEADTOOLS License

      using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.LEAD)) 
      using (DocumentConverter docConverter = new DocumentConverter()) 
      { 
        ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS21\Bin\Common\OcrLEADRuntime"); 
        docConverter.SetOcrEngineInstance(ocrEngine, false); 
        // Change the DocumentFormat enumeration to whichever format is needed 
        ConvertFile(input_file, output_file, docConverter, DocumentFormat.Pdf); 
      } 
    } 
  } 
} 

Finally, convert the document.

static void ConvertFile(string inFile, string outFile, DocumentConverter docConverter, DocumentFormat docFormat) 
{ 
  Console.WriteLine($"Converting {inFile}..."); 
  DocumentConverterJobData jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, docFormat); 
  jobData.JobName = "Convert to Webpage Job"; 
  DocumentConverterJob job = docConverter.Jobs.CreateJob(jobData); 
  docConverter.Jobs.RunJob(job); 
  if (job.Errors.Count > 0) 
    foreach (var error in job.Errors) 
      Console.WriteLine($"Error during conversion: {error.Error.Message}\n"); 
  else 
    Console.WriteLine($"Successfully Convereted {inFile} to {outFile}\n"); 
} 

The above code could easily be modified to convert the input file to one of LEAD’s supported document formats by changing the document format enumeration.

Convert HTML to PDF in VB

Using documentConverter As DocumentConverter = New DocumentConverter()
  Dim inFile = Path.Combine(ImagesPath.Path, "example.html")
  Dim outFile = Path.Combine(ImagesPath.Path, "example.pdf")
  Dim format = DocumentFormat.Pdf
  Dim jobData = DocumentConverterJobs.CreateJobData(inFile, outFile, format)
  jobData.JobName = "TXT conversion to PDF"
  Dim documentWriter = New DocumentWriter()
  documentConverter.SetDocumentWriterInstance(documentWriter)
  Dim job = documentConverter.Jobs.CreateJob(jobData)
  documentConverter.Jobs.RunJob(job)

  If job.Status = DocumentConverterJobStatus.Success Then
    Console.WriteLine("Success")
  Else
    Console.WriteLine("{0} Errors", job.Status)

    For Each [error] In job.Errors
      Console.WriteLine("  {0} at {1}: {2}", [error].Operation, [error].InputDocumentPageNumber, [error].[Error].Message)
    Next
  End If
End Using

More information about this example can be found in the Document Converter class documentation.

Convert HTML to PDF in Java

String inFile = Path.Combine(ImagesPath.Path, @"example.html");
String outFile = Path.Combine(ImagesPath.Path, @"example.pdf");
DocumentFormat format = DocumentFormat.Pdf;
DocumentConverter documentConverter = new DocumentConverter();
DocumentWriter docWriter = new DocumentWriter();
docConverter.setDocumentWriterInstance(docWriter);
DocumentConverterJobData jobData =
  DocumentConverterJobs.CreateJobData(inFile, outFile, format);
jobData.setJobName("HTML Conversion");
DocumentConverterJob job = docConverter.getJobs().createJob(jobData);
docConverter.getJobs().runJob(job);
if (job.getErrors().size() > 0)
  for (DocumentConverterJobError error : job.getErrors())
    System.out.println("%2fnError during conversion: " + error.getError().getMessage());
else
  System.out.println("Successfully converted file to " + outFile);

For more information on the Java Document Converter, check out our full tutorial on the LEADTOOLS Java Document Converter.

Try for Free

Download the LEADTOOLS SDK for free. It’s fully-functional for 60 days, and comes with free chat and email support.

LEAD Is Here For You

Stay tuned for more conversion examples to see how the LEADTOOLS document converter will easily fit into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team via email or call us at 704-332-5532.

This entry was posted in Document Converter and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *