Skip to main content

How to convert HTML to PDF in Java? Efficient conversion, Steps & Java code. Over 22 Years in PDF Libraries.

HTML to PDF in Java

HTML to PDF conversion can be done using the DynamicPDF Generator product.

How to Convert HTML to PDF​

The HtmlLayout class can be used to convert HTML to PDF using Generator with several custom options for page info, header and footer for the output PDF.

Steps for Creating a PDF Document using Generator​

  1. Create PageInfo with required page options.
  2. Create a URI object to point to the HTML file.
  3. Create a HtmlLayout object by specifying the URI and PageInfo.
  4. Create a Header and Footer as required to the HtmlLayout object.
  5. Convert the URI to Document by calling Layout method and save the Document.

Sample code - C#​

PageInfo layoutPage = new PageInfo(PageSize.A4, PageOrientation.Portrait);
Uri uri = new Uri(@"http://www.google.com");

HtmlLayout html = new HtmlLayout(uri, layoutPage);

html.Header.Center.Text = "%%PR%%%%SP%% of %%ST%%";
html.Header.Center.HasPageNumbers = true;
html.Header.Center.Width = 200;

html.Footer.Center.Text = "%%PR%%%%SP(A)%% of %%ST(B)%%";
html.Footer.Center.HasPageNumbers = true;
html.Footer.Center.Width = 200;

Document document = html.Layout();
document.Draw(@"Output.pdf");

Getting Started​

DynamicPDF Generator Information​

More information can be found here:

Available in other platforms​

DynamicPDF Generator is available for the following platforms. Refer to the respective product pages for more details.