{"id":13722341,"url":"https://github.com/chuongtrh/html_to_pdf","last_synced_at":"2025-10-10T00:12:30.089Z","repository":{"id":37699915,"uuid":"151382762","full_name":"chuongtrh/html_to_pdf","owner":"chuongtrh","description":"Generate a simple invoice PDF from HTML using puppeteer \u0026 handlebars","archived":false,"fork":false,"pushed_at":"2025-06-07T05:10:18.000Z","size":509,"stargazers_count":116,"open_issues_count":0,"forks_count":38,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-29T09:08:03.447Z","etag":null,"topics":["handlebars","htmltopdf","pdf","puppeteer"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@chuongtrh/html-to-pdf","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chuongtrh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2018-10-03T08:29:56.000Z","updated_at":"2025-06-07T05:10:22.000Z","dependencies_parsed_at":"2024-09-25T00:31:37.516Z","dependency_job_id":"344c81fe-fe0d-449f-ad76-2e67d8957837","html_url":"https://github.com/chuongtrh/html_to_pdf","commit_stats":{"total_commits":15,"total_committers":4,"mean_commits":3.75,"dds":"0.19999999999999996","last_synced_commit":"01feca79521f82679e21bb8381f0e5a57745213d"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/chuongtrh/html_to_pdf","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chuongtrh%2Fhtml_to_pdf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chuongtrh%2Fhtml_to_pdf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chuongtrh%2Fhtml_to_pdf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chuongtrh%2Fhtml_to_pdf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chuongtrh","download_url":"https://codeload.github.com/chuongtrh/html_to_pdf/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chuongtrh%2Fhtml_to_pdf/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279002373,"owners_count":26083356,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["handlebars","htmltopdf","pdf","puppeteer"],"created_at":"2024-08-03T01:01:27.587Z","updated_at":"2025-10-10T00:12:30.084Z","avatar_url":"https://github.com/chuongtrh.png","language":"JavaScript","funding_links":[],"categories":["Examples"],"sub_categories":[],"readme":"## html_to_pdf\n\nGenerate beautiful PDF invoices from HTML using [Puppeteer](https://github.com/GoogleChrome/puppeteer) \u0026 [Handlebars](http://handlebarsjs.com/).  \n**Modern, flexible, and easy to use!**\n\n![Invoice](https://raw.githubusercontent.com/chuongtrh/html_to_pdf/master/screenshot/invoice.png)\n\n---\n\n## ⭐ Features\n\n- **Modern rendering:** Uses real Chrome engine for pixel-perfect PDFs\n- **Dynamic templates:** Handlebars for easy data binding\n- **Watermark/stamp support:** Add \"PAID\" or custom stamps with a simple flag\n- **Full CSS/JS support:** Works with modern HTML, CSS, and JavaScript\n- **Easy integration:** Just a few lines of code to get started\n\n---\n\n## 🚀 Quick Start\n\n1. **Install dependencies**\n   ```sh\n   npm install\n   ```\n2. **Generate your first PDF**\n   ```sh\n   node example.js\n   ```\n   This will create `invoice.pdf` in your project folder.\n\n---\n\n## How It Works\n\n1. Prepare your HTML template (`invoice.html`)\n2. Bind your data using Handlebars\n3. Generate PDF with Puppeteer\n\n---\n\n## Example Usage\n\n**index.js**\n```js\nconst puppeteer = require(\"puppeteer\");\nconst handlebars = require(\"handlebars\");\n\nmodule.exports.html_to_pdf = async ({ templateHtml, dataBinding, options }) =\u003e {\n  const template = handlebars.compile(templateHtml);\n  const finalHtml = encodeURIComponent(template(dataBinding));\n\n  const browser = await puppeteer.launch({\n    args: [\"--no-sandbox\"],\n    headless: true,\n  });\n  const page = await browser.newPage();\n  await page.goto(`data:text/html;charset=UTF-8,${finalHtml}`, {\n    waitUntil: \"networkidle0\",\n  });\n  await page.pdf(options);\n  await browser.close();\n};\n```\n\n**example.js**\n```js\nconst fs = require(\"fs\");\nconst path = require(\"path\");\nconst { html_to_pdf } = require(\".\");\n\n(async () =\u003e {\n  const dataBinding = {\n    items: [\n      { name: \"item 1\", price: 100 },\n      { name: \"item 2\", price: 200 },\n      { name: \"item 3\", price: 300 },\n    ],\n    total: 600,\n    isWatermark: true,\n  };\n\n  const templateHtml = fs.readFileSync(\n    path.join(process.cwd(), \"invoice.html\"),\n    \"utf8\"\n  );\n\n  const options = {\n    format: \"A4\",\n    headerTemplate: \"\u003cp\u003e\u003c/p\u003e\",\n    footerTemplate: \"\u003cp\u003e\u003c/p\u003e\",\n    displayHeaderFooter: false,\n    margin: { top: \"40px\", bottom: \"100px\" },\n    printBackground: true,\n    path: \"invoice.pdf\",\n  };\n\n  await html_to_pdf({ templateHtml, dataBinding, options });\n\n  console.log(\"Done: invoice.pdf is created!\");\n})();\n```\n\n---\n\n## 💡 Watermark/Stamp Example\n\nAdd a \"PAID\" stamp using Handlebars:\n\n```handlebars\n{{#if isWatermark}}\n  \u003cdiv style=\"border-width: 6px; border-style: solid; border-color: #008000; border-radius: 8px; color: #008000; opacity:0.6; position: absolute; z-index: 1; left:40%; top:30%; font-size: 60pt; transform: rotate(-45deg); font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;\"\u003e\n    PAID\n  \u003c/div\u003e\n{{/if}}\n```\nSet `isWatermark: true` in your data and run again!\n\n![Invoice with stamp paid watermark](https://raw.githubusercontent.com/chuongtrh/html_to_pdf/master/screenshot/invoice_paid.png)\n\n---\n\n## 🆚 Comparison: Puppeteer vs. wkhtmltopdf vs. dompdf\n\n| Feature                | Puppeteer (This Project) | wkhtmltopdf                | dompdf (PHP)              |\n|------------------------|-------------------------|----------------------------|---------------------------|\n| Language               | Node.js                 | Standalone (CLI, C++)      | PHP                       |\n| Rendering Engine       | Headless Chrome         | WebKit                     | PHP-based HTML parser     |\n| CSS/JS Support         | Full (modern browser)   | Limited (older WebKit)     | Limited (no JS)           |\n| JavaScript Execution   | Yes                     | Partial                    | No                        |\n| Template Engine        | Handlebars (JS)         | None (raw HTML)            | None (raw HTML)           |\n| Custom Fonts           | Yes                     | Yes                        | Yes                       |\n| Page Features          | Headers/Footers, Margins| Headers/Footers, Margins   | Basic                     |\n| Watermark/Stamp        | Easy (template logic)   | Possible (CSS/HTML)        | Possible (CSS/HTML)       |\n| Installation           | `npm install puppeteer` | Download binary            | Composer (PHP)            |\n| Platform               | Cross-platform          | Cross-platform             | Cross-platform            |\n| Output Format          | PDF                     | PDF                        | PDF                       |\n\n### Pros \u0026 Cons\n\n#### Puppeteer (This Project)\n**Pros:**\n- Accurate rendering (uses real Chrome engine)\n- Full support for modern CSS and JavaScript\n- Highly customizable with Node.js and Handlebars\n- Easy watermark/stamp logic\n- Active community and frequent updates\n\n**Cons:**\n- Requires Node.js environment\n- Larger binary size due to Chromium\n- Higher memory usage compared to simpler tools\n\n#### wkhtmltopdf\n**Pros:**\n- Simple CLI usage, easy to integrate in scripts\n- No programming language dependency\n- Good for static HTML to PDF conversion\n\n**Cons:**\n- Uses outdated WebKit engine (limited CSS/JS support)\n- JavaScript support is partial and sometimes buggy\n- Less accurate rendering for modern web pages\n\n#### dompdf\n**Pros:**\n- Native PHP solution, easy for PHP projects\n- Simple to install via Composer\n- Good for basic HTML/CSS to PDF\n\n**Cons:**\n- No JavaScript support\n- Limited CSS support (no flexbox, grid, etc.)\n- Rendering may differ from browsers\n\n**Summary:**  \nChoose **Puppeteer** if you need modern web features, dynamic templates, or advanced PDF customization. Use **wkhtmltopdf** for simple CLI-based workflows, or **dompdf** for basic PHP-based PDF generation.\n\n---\n\n## 🙌 Contributing\n\nPull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.\n\n---\n\n## 📣 Spread the Word\n\nIf you find this project useful, please ⭐ star the repo and share it with others!\n\n---\n\n## License\n\nhtml_to_pdf is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchuongtrh%2Fhtml_to_pdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchuongtrh%2Fhtml_to_pdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchuongtrh%2Fhtml_to_pdf/lists"}