gitmyhub

url-to-pdf-api

HTML ★ 7.1k updated 2y ago

Web page PDF/PNG rendering done right. Self-hosted service for rendering receipts, invoices, or any content.

API service that converts any URL or raw HTML into a PDF or screenshot image using headless Chrome, send a URL, get back a pixel-perfect PDF or PNG, great for invoices, receipts, and reports.

Node.jsJavaScriptPuppeteerChromesetup: moderatecomplexity 3/5

This project is a small API service that converts web pages into PDF files or screenshot images. You give it a URL or raw HTML, and it returns a rendered PDF or PNG, using a full instance of Google Chrome running in the background without a visible window (called headless Chrome). The result matches what you would get if you opened the page in a real Chrome browser and printed it to PDF.

The service is built primarily for generating documents like receipts, invoices, and reports. Any web page that can be opened in Chrome can be rendered, including single-page applications that load their content via JavaScript. An option called scrollPage triggers the browser to scroll the page to the bottom before rendering, which helps capture pages that load images or content only as the user scrolls down.

Configuration is done through URL query parameters or a JSON body in a POST request. You can control the output format (PDF or image), page size, margins, orientation, whether to emulate print or screen styles, and how long to wait before capturing. The API uses Puppeteer internally, a Node.js library that controls Chrome programmatically. Page size, margins, and scale are all adjustable.

The README includes a prominent security warning: because the service runs JavaScript inside a real Chrome session on the server, it could be used to read files from the host machine. It should not be exposed to the public internet without careful access controls.

Deployment to Heroku is available with a one-click button. Running it locally requires Node.js and access to Chrome. A Docker image from a third party is also referenced in the README.

Where it fits