October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Convert HTML to PDF with DocRaptor

Use DocRaptor’s authenticated API to turn HTML or a retrievable URL into a PDF, with practical request examples, rendering guidance, security notes, and fixes for common errors.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To convert HTML to PDF with DocRaptor, send an authenticated JSON POST request to https://api.docraptor.com/docs with type set to pdf and either document_content or document_url. For a successful synchronous request, save the binary response as a PDF; check the HTTP status first so an error response is not mistaken for a document.

Choose HTML content or a document URL

Use document_content when your application already has the HTML string. Use document_url when the document is available at a URL DocRaptor can retrieve. A PDF request needs type: "pdf" and one of these inputs.

As an Amazon Associate I earn from qualifying purchases.

Input Use it when What to check
document_content Your application has the HTML or XML content. If the supplied markup uses relative asset URLs, set a base URL, for example with prince_options.baseurl.
document_url The document is served from a retrievable URL. DocRaptor must be able to fetch the page and any assets it needs.

DocRaptor’s [conversion guide] shows the prince_options.baseurl approach for content with relative links. Validate asset loading with your actual document and consult the current API reference for supported request fields.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Send a PDF request from your server

The following Python example posts HTML directly, authenticates with HTTP Basic Authentication using the API key as the username and a blank password, checks for an HTTP error, and writes the returned bytes to invoice.pdf. Keep the real key in a server-side secret or environment variable; do not put it in client-side code.

#1 Best Overall
PDF Extra 2024| Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Lifetime License | 1 Windows PC | 1 User [PC Online code]
  • EDIT text, images & designs in PDF documents. ORGANIZE PDFs. Convert PDFs to Word, Excel & ePub.
  • READ and Comment PDFs – Intuitive reading modes & document commenting and mark up.
  • CREATE, COMBINE, SCAN and COMPRESS PDFs
  • FILL forms & Digitally Sign PDFs. PROTECT and Encrypt PDFs
  • LIFETIME License for 1 Windows PC or Laptop. 5GB MobiDrive Cloud Storage Included.
import os
import requests

api_key = os.environ["DOCRAPTOR_API_KEY"]
html = "<html><body><h1>Invoice</h1></body></html>"

response = requests.post(
    "https://api.docraptor.com/docs",
    auth=(api_key, ""),
    json={"type": "pdf", "document_content": html},
    timeout=120,
)
response.raise_for_status()

with open("invoice.pdf", "wb") as pdf_file:
    pdf_file.write(response.content)

print("Saved invoice.pdf")

Install the dependency with python -m pip install requests. For URL input, replace the document_content member with "document_url": "https://example.com/invoice". The endpoint, authentication method, and binary response behavior are described in DocRaptor’s API overview.

Equivalent cURL request

curl --user "$DOCRAPTOR_API_KEY:" 
  --header "Content-Type: application/json" 
  --data '{"type":"pdf","document_content":"<html><body><h1>Invoice</h1></body></html>"}' 
  "https://api.docraptor.com/docs" 
  --output invoice.pdf

In cURL’s Basic Authentication syntax, the colon after the key supplies the blank password. The example writes the response body to a file, so check the HTTP status as well when adapting it for production; otherwise an error body could be saved with a .pdf extension.

Rank #2
MobiPDF Lifetime - Professional PDF Editor for Windows | Edit, Sign & Convert PDFs | Best Adobe Acrobat Pro Alternative | Lifetime License
  • Edit PDFs with Ease. Modify text, images, and layouts directly within your PDF documents.
  • Convert & Organize. Export PDFs to Word, Excel, or ePub, and organize files with ease.
  • Read & Annotate. Enjoy intuitive reading modes and powerful tools to comment, highlight, and mark up PDFs.
  • Create & Manage PDFs. Create new PDFs, combine multiple files, scan documents, and compress for easy sharing.
  • Fill & Sign Forms. Complete forms and digitally sign documents with secure e-signature tools.

Enable JavaScript only when the document needs it

JavaScript execution is disabled by default. Leave it off for static documents; enable the documented JavaScript option when the PDF depends on content generated by scripts, such as a chart. DocRaptor’s JavaScript execution guide describes two engines and recommends its primary engine for most users. Enabling both evaluates the page’s JavaScript twice, so choose the engine your document requires rather than turning on both by default.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If JavaScript fills in content asynchronously, conversion must wait until rendering is complete. DocRaptor documents a docraptorJavaScriptFinished() function that signals completion. JavaScript errors can also cause document creation to fail. See its HTML and JavaScript tutorial for the current configuration and completion behavior.

Rank #3
Adobe Acrobat Pro | PDF Software | Convert, Edit, E-Sign, Protect | PC/Mac Online Code | Activation Required
  • Create and edit PDFs. Collaborate with ease. E-sign documents and collect signatures. Get everything done in one app, wherever you go.
  • Edit text and images without jumping to another app.
  • E-sign documents or request e-signatures on any device. Recipients don’t need to log in to e-sign.
  • Convert PDFs to editable Microsoft Word, Excel, or PowerPoint documents.
  • Share PDFs for collaboration. Commenting features make it easy for reviewers to comment, mark up, and annotate.

Set print styling and page layout

Use CSS to define the layout you want on paper. DocRaptor’s examples include prince_options.media to choose between screen and print styles. If your site has print-specific rules, select print media; if it relies on screen styles, select screen media. Do not assume every browser-specific CSS behavior will render identically in PDF: inspect representative output and consult DocRaptor’s current style and formatting documentation.

Handle the response correctly

For a successful synchronous request, the response body is the PDF’s binary data. Save it as bytes, as in the examples above, or pass those bytes through your server to a browser download. DocRaptor documents other workflows with different response handling: hosted generation returns a public URL, and asynchronous generation returns a status identifier that you use to retrieve the completed document. Choose those modes when your application needs hosted delivery or a separate generation-and-retrieval flow.

Rank #4
PDF Extra Lifetime - Professional PDF Editor - Best Adobe Acrobat Pro Alternative - Lifetime License for Windows PC
  • Perfect Adobe Acrobat Pro alternative – lifetime license for Windows 10 and 11.
  • EDIT text, images, pages, hyperlinks, designs in PDF documents. ORGANIZE PDFs.
  • READ and Comment on PDFs – Intuitive reading modes & document commenting and mark up tools!
  • CREATE, COMBINE, SCAN and COMPRESS PDFs.
  • FILL forms & Digitally Sign PDFs. Work with Digital certificates

Check the HTTP status before treating the body as a PDF. DocRaptor says generation errors return XML, and its API overview explains that status codes indicate success or failure. For PDF responses, the X-DocRaptor-Num-Pages header reports the page count. See the API overview for response details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep the API key out of public browser code

A browser-side integration can expose its API key to anyone who can inspect the page. DocRaptor’s tutorial warns that its hidden-form browser approach still exposes the key in page source and is not suitable for publicly accessible use. Prefer a server-side endpoint that authenticates with the key and returns the generated PDF to your application. DocRaptor also describes a referrer-based workflow; assess that option against your access-control needs before using it. See the official tutorial.

Best Value
PDF Pro 3 - PDF editor to create, edit, convert and merge PDFs - 100% Compatible with Adobe Acrobat - for Windows 11, 10, 8.1, 7
  • ALL-IN-ONE SOLUTION – read, edit, convert, merge and protect your PDF files
  • MAXIMUM FUNCIONALITY – create interactive forms, compare PDFs, bates numbering, find and replace text or colors, convert documents, OCR engine, comment, highlight, fill out and print forms, document protection and others
  • EASY TO INSTALL AND USE – well-structured user-interface, in-program instructions, free tech support whenever you need it
  • GREAT VALUE FOR MONEY - why spend a fortune if you can have maximum functionality at a reasonable price - this also fits the requirements of companies very well
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common conversion failures

  • The saved file is not a PDF: Check the HTTP status and response before saving or serving the body. Error responses can contain XML rather than PDF data.
  • The request is rejected or lacks a document: Confirm the JSON includes "type": "pdf" and exactly one usable input, document_content or document_url.
  • Images, stylesheets, or fonts are missing: For content supplied as a string, configure an appropriate base URL for relative links. For URL input, ensure DocRaptor can retrieve the page and its required assets.
  • Script-generated content is absent: JavaScript is off by default. Enable the required engine and ensure delayed rendering signals completion with the documented mechanism.
  • Document generation fails after enabling scripts: Check for JavaScript errors and whether the selected engine matches the page’s needs; using both engines runs the page’s JavaScript twice.
  • The API key appears in page source: Move authenticated generation to a server-side request path or evaluate DocRaptor’s referrer-based approach instead of exposing a production key in public code.

Or skip the browser setup

If your task is to capture a web page as an image or PDF rather than generate a formatted document through DocRaptor, ScreenshotNeo is a website screenshot API and MCP server for developers. Its one-call API captures a URL; for a WebP screenshot:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. ScreenshotNeo accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each of those steps can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers report the page verdict and whether the request was billed. Its MCP server lets AI agents use take_screenshot, get_page_info, and capture_pdf. The free plan includes 1,000 screenshots a month without a card; paid plans start at $5 for 3,000 shots. Sign up free for ScreenshotNeo.

Frequently Asked Questions

Can DocRaptor return a PDF from an HTML string instead of a URL?

Yes. Set document_content to the HTML string and set type to pdf.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How can I tell how many pages the generated PDF contains?

For PDF responses, DocRaptor documents the X-DocRaptor-Num-Pages response header.

Quick Recap

Bestseller No. 1
PDF Extra 2024| Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Lifetime License | 1 Windows PC | 1 User [PC Online code]
PDF Extra 2024| Complete PDF Reader and Editor | Create, Edit, Convert, Combine, Comment, Fill & Sign PDFs | Lifetime License | 1 Windows PC | 1 User [PC Online code]
READ and Comment PDFs – Intuitive reading modes & document commenting and mark up.; CREATE, COMBINE, SCAN and COMPRESS PDFs
$99.99
Bestseller No. 2
MobiPDF Lifetime - Professional PDF Editor for Windows | Edit, Sign & Convert PDFs | Best Adobe Acrobat Pro Alternative | Lifetime License
MobiPDF Lifetime - Professional PDF Editor for Windows | Edit, Sign & Convert PDFs | Best Adobe Acrobat Pro Alternative | Lifetime License
Edit PDFs with Ease. Modify text, images, and layouts directly within your PDF documents.; Convert & Organize. Export PDFs to Word, Excel, or ePub, and organize files with ease.
$99.99
Bestseller No. 3
Adobe Acrobat Pro | PDF Software | Convert, Edit, E-Sign, Protect | PC/Mac Online Code | Activation Required
Adobe Acrobat Pro | PDF Software | Convert, Edit, E-Sign, Protect | PC/Mac Online Code | Activation Required
Edit text and images without jumping to another app.; Convert PDFs to editable Microsoft Word, Excel, or PowerPoint documents.
$239.88
Bestseller No. 4
PDF Extra Lifetime - Professional PDF Editor - Best Adobe Acrobat Pro Alternative - Lifetime License for Windows PC
PDF Extra Lifetime - Professional PDF Editor - Best Adobe Acrobat Pro Alternative - Lifetime License for Windows PC
Perfect Adobe Acrobat Pro alternative – lifetime license for Windows 10 and 11.; EDIT text, images, pages, hyperlinks, designs in PDF documents. ORGANIZE PDFs.
$99.99
Bestseller No. 5
PDF Pro 3 - PDF editor to create, edit, convert and merge PDFs - 100% Compatible with Adobe Acrobat - for Windows 11, 10, 8.1, 7
PDF Pro 3 - PDF editor to create, edit, convert and merge PDFs - 100% Compatible with Adobe Acrobat - for Windows 11, 10, 8.1, 7
ALL-IN-ONE SOLUTION – read, edit, convert, merge and protect your PDF files
$29.99

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.