{"id":16964357,"url":"https://github.com/beyluta/html-template-to-pdf","last_synced_at":"2025-04-14T13:44:38.581Z","repository":{"id":75204469,"uuid":"604799364","full_name":"beyluta/html-template-to-pdf","owner":"beyluta","description":"Cross-platform library that converts a html template file into a pdf. ","archived":false,"fork":false,"pushed_at":"2023-03-20T11:13:57.000Z","size":36,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-28T02:53:02.548Z","etag":null,"topics":["html-template","html-templating","npm-package","pdf","pdf-generation","pdf-templating"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beyluta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2023-02-21T20:16:47.000Z","updated_at":"2023-09-09T20:26:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"fe534115-a029-4f78-a9ae-9fc80932a1cb","html_url":"https://github.com/beyluta/html-template-to-pdf","commit_stats":{"total_commits":22,"total_committers":2,"mean_commits":11.0,"dds":"0.13636363636363635","last_synced_commit":"4e0756c79f6c49d949ceda4a918655fa2b73978f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyluta%2Fhtml-template-to-pdf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyluta%2Fhtml-template-to-pdf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyluta%2Fhtml-template-to-pdf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beyluta%2Fhtml-template-to-pdf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beyluta","download_url":"https://codeload.github.com/beyluta/html-template-to-pdf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248890371,"owners_count":21178415,"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","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":["html-template","html-templating","npm-package","pdf","pdf-generation","pdf-templating"],"created_at":"2024-10-13T23:43:21.503Z","updated_at":"2025-04-14T13:44:38.558Z","avatar_url":"https://github.com/beyluta.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# html-template-to-pdf\n\nCross-platform library that converts a html file into a pdf.\n\nTake a look at [`html-pdf-node package`](https://github.com/mrafiqk/html-pdf-node) for additional pdf generation options.\n\n# Installation\n\n```\nnpm install html-template-to-pdf\n```\n\n# Usage\n\nHere is an example of how to generate a pdf from `index.html`.\n\n```javascript\nconst fs = require(\"fs\");\nconst generatePDF = require(\"html-template-to-pdf\");\n\nasync function main() {\n  const arrayBuffer = await generatePDF(\"index.html\");\n  const fileStream = fs.createWriteStream(\"output.pdf\");\n  fileStream.write(Buffer.from(arrayBuffer));\n  fileStream.end();\n}\n\nmain();\n```\n\n# Templating\n\n### Replacing placeholders\n\nThe library allows you to use templating to generate dynamic pdfs. It uses the second argument of the `generatePDF` function. Here is an example of how to use it.\n\n```javascript\nconst fs = require(\"fs\");\nconst generatePDF = require(\"html-template-to-pdf\");\n\nasync function main() {\n  const arrayBuffer = await generatePDF(\"index.html\", { str: \"World!\" });\n  const fileStream = fs.createWriteStream(\"output.pdf\");\n  fileStream.write(Buffer.from(arrayBuffer));\n  fileStream.end();\n}\n\nmain();\n```\n\nThe HTML file `index.html` should look like this.\n\n```html\n\u003cp\u003eHello { str }\u003c/p\u003e\n```\n\nThe output pdf will look like this:\n\n```\nHello World!\n```\n\n# Pseudo conditional templating\n\n### Showing or hiding elements\n\nYou can also use pseudo conditional templating to hide or show elements. Here is an example of how to use it.\n\n```javascript\nconst fs = require(\"fs\");\nconst generatePDF = require(\"html-template-to-pdf\");\n\nasync function main() {\n  /*\n   **  When `show` is true, the element will be shown.\n   **  When `show` is false, the element will be hidden.\n   */\n  const arrayBuffer = await generatePDF(\"index.html\", { show: true });\n  const fileStream = fs.createWriteStream(\"output.pdf\");\n  fileStream.write(Buffer.from(arrayBuffer));\n  fileStream.end();\n}\n\nmain();\n```\n\nThe HTML file `index.html` should look like this.\n\n```html\n\u003cp\u003eHello ?{show World! }?\u003c/p\u003e\n```\n\nThe output pdf will look like this:\n\n```\nHello World!\n```\n\nOr if `show` is false, the output pdf will look like this:\n\n```\nHello\n```\n\n### Negative conditions\n\nUse the prefix `!` in front of the condition to make it negative. In the following example: the element will only be visible if `show` is false\n```html\n\u003cp\u003eHello ?{!show World! }?\u003c/p\u003e\n```\n\n# Example\n\n```javascript\nconst fs = require(\"fs\");\nconst generatePDF = require(\"html-template-to-pdf\");\n\nasync function main() {\n  const arrayBuffer = await generatePDF(\"index.html\", {\n    employeeName: \"John Doe\",\n    salary: \"$9000\",\n    show: true,\n  });\n  const fileStream = fs.createWriteStream(\"output.pdf\");\n  fileStream.write(Buffer.from(arrayBuffer));\n  fileStream.end();\n}\n\nmain();\n```\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n  \u003cbody\u003e\n    \u003cdiv\u003e\n      \u003c!-- The div will only be visible if `show` is set --\u003e\n      ?{show\n      \u003cdiv\u003e\n        \u003cp\u003eHello, { employeeName }\u003c/p\u003e\n\n        \u003c!-- The paragraph will be shown if `salary` is set. Note that `salary` can be of any type: Boolean, Number, Float, String, ...  --\u003e\n        ?{salary\n        \u003cp\u003eYour salary is { salary }\u003c/p\u003e\n        }?\n      \u003c/div\u003e\n      }?\n    \u003c/div\u003e\n  \u003c/body\u003e\n\u003c/html\u003e\n```\n\nWould generate:\n\n```\nHello, John Doe\nYour salary is $9000\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeyluta%2Fhtml-template-to-pdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeyluta%2Fhtml-template-to-pdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeyluta%2Fhtml-template-to-pdf/lists"}