Skip to main content

Convert HTML to PDF


Easily convert HTML to PDFs using The DynamicPDF API's pdf endpoint.

Convert HTML to PDFs using the pdf endpoint. Call the pdf endpoint directly or using one of our client libraries.

tip

Check out Getting Started and Task Roadmap if you are new to The DynamicPDF API.

Calling Endpoint Directly

Convert from HTML to a PDF using the html element in a JSON instructions document. The following example illustrates the steps in converting a simple HTML file to a PDF.

  • Create an instructions.json document that specifies html as the input and the HTML document as the resourceName or add HTML directly to the instructions by specifying the htmlString rather than resourceName.

The following fragment first converts an HTML string to a PDF, then the HTML document, products.html to a PDF, and then merges the two together to form a combined PDF.

{
"inputs": [
{
"type": "html",
"htmlString": "<html>An example HTML fragment.</html>"
},
{
"type": "html",
"resourceName":"products.html"
}
]
}
  • Add the authorization key as a header.
  • Add the Instructions document as a file and the HTML document as a file.
curl --location 'https://api.dpdf.io/v1.0/pdf'
--header 'Authorization: Bearer --api-key--'
--form 'Instructions=@C:/temp/solutions/html-pdf/instructions.json'
--form 'Resource=@C:/temp/solutions/html-pdf/products.html'

Run In Postman

tip

See the Merge PDFs and Other Resources for more information on how you can merge multiple HTML documents and other resources into a combined PDF.

Calling Endpoint Using Client Library

Easily convert HTML to a PDF using the C#, Java, Node.js, PHP, Go, or Python client library.

The following example illustrates using HTML to create a PDF.

HtmlResource resource = new HtmlResource(System.IO.File.ReadAllText(basePath + "products.html"));
pdf.AddHtml(resource);
Source: InstructionsExample.cs
info

The pdf endpoint takes a JSON instructions document that provides instructions for creating, transforming, merging, and formatting inputs into a combined PDF. Refer to documentation on the instructions schema for information on how to use the pdf endpoint.

   Follow us on social media for latest news!