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.

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.jsondocument that specifieshtmlas the input and the HTML document as theresourceNameor add HTML directly to the instructions by specifying thehtmlStringrather thanresourceName.
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'
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.
- C# (.NET)
- Java
- Node.js
- PHP
- Go
- Python
- Ruby
The following example illustrates using HTML to create a PDF.
HtmlResource resource = new HtmlResource(System.IO.File.ReadAllText(basePath + "products.html"));
pdf.AddHtml(resource);
The following example illustrates using HTML to create a PDF.
static async HtmlExample(basePath) {
var pdf = new Pdf();
pdf.addHtml("<html>An example HTML fragment.</html>");
pdf.addHtml("<html><p>HTML with basePath.</p><img src='./images/logo.png'></img></html>",
"https://www.dynamicpdf.com", PageSize.LETTER, Orientation.PORTRAIT,1);
var resourcePath = basePath + "/products.html";
pdf.addHtml(new HtmlResource(resourcePath), null, PageSize.LETTER, Orientation.PORTRAIT, 1);
await this.ProcessAndSave(pdf, "html-output.pdf");
}
The following example illustrates using HTML to create a PDF.
Pdf pdf = new Pdf();
pdf.AddHtml("<html>An example HTML fragment.</html>", null, PageSize.LETTER, PageOrientation.PORTRAIT, 1F);
// use basepath in an HTML string, note the image is accessible via WWW. Note the embedded CSS.
pdf.addHtml("<html><p style='color:red;font-family:verdana;font-size:30px'>HTML with basePath.</p><img src='./images/logo.png'></img></html>", "https://www.dynamicpdf.com", PageSize.LETTER, PageOrientation.PORTRAIT, 1F);
// add html from a path on local drive
String temp = null;
try {
temp = Files.readString(Paths.get(basePath + "products.html"));
} catch (IOException e) {
e.printStackTrace();
}
HtmlResource resource = new HtmlResource(temp);
pdf.AddHtml(resource, null, PageSize.LETTER, PageOrientation.PORTRAIT, 1F);
return pdf;
The following example illustrates using HTML to create a PDF.
$pdf = new Pdf();
$pdf->AddHtml("<html><p>This is a test.</p></html>");
$resource = new HtmlResource($basePath . "HtmlWithAllTags.html");
$pdf->AddHtml($resource);
$pdf->AddHtml("<html><img src='./images/logo.png'></img></html>", "https://www.dynamicpdf.com");
return $pdf;
The following example illustrates using HTML to create a PDF.
pdfExample := endpoint.NewPdf()
pdfExample.Endpoint.BaseUrl = "https://api.dpdf.io/"
pdfExample.Endpoint.ApiKey = "DP.xxx-api-key-xxx"
inputOne := input.NewHtmlInputWithString("<html>An example HTML fragment.</html>")
pdfExample.Inputs = append(pdfExample.Inputs, inputOne)
resp := pdfExample.Process()
res := <-resp
if res.IsSuccessful() == true {
os.WriteFile("c:/temp/html-to-pdf/html-output-go.pdf", res.Content().Bytes(), os.ModeType)
} else {
fmt.Print(res.ErrorJson())
}
The following example illustrates using HTML to create a PDF.
def ug_html_example(documentPath):
pdf = Pdf()
pdf.add_html("<html><p>Welcome to DynamicPDF API.</p></html>")
with open(documentPath + "products.html","rt") as f:
fileData = f.read()
f.close()
pdf.add_html(fileData)
pdf.add_html("<html><img src='./images/logo.png'></img></html>", "https://www.dynamicpdf.com")
return pdf
The following example illustrates using HTML to create a PDF.
def self.html_to_pdf(base_path)
pdf = Pdf.new
pdf.add_html('<html><p>This is a test.</p></html>')
file_data = Utility.get_file_data("#{base_path}HtmlWithAllTags.html")
resource = HtmlResource.new(file_data)
pdf.add_html(resource)
pdf.add_html("<html><img src='./images/logo.png'></img></html>", "https://www.dynamicpdf.com")
pdf
end
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.