{"_id":"@0x170818/html2pdf","name":"@0x170818/html2pdf","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@0x170818/html2pdf","version":"1.0.0","description":"Client-side HTML-to-PDF rendering using pure JS","main":"dist/html2pdf.js","repository":{"type":"git","url":"git+https://github.com/eKoopmans/html2pdf.js.git"},"keywords":["javascript","pdf-generation","html","client-side","canvas"],"author":{"name":"Erik Koopmans","email":"erik@erik-koopmans.com","url":"https://www.erik-koopmans.com"},"license":"MIT","bugs":{"url":"https://github.com/eKoopmans/html2pdf.js/issues"},"homepage":"https://ekoopmans.github.io/html2pdf.js/","dependencies":{"es6-promise":"^4.2.5","@0x170818/html2canvas":"^1.0.0","@0x170818/jspdf":"^2.5.0"},"devDependencies":{"@babel/core":"^7.14.8","@babel/preset-env":"^7.14.8","babel-loader":"^8.2.2","chai":"^4.2.0","chai-spies":"^1.0.0","commander":"^7.2.0","core-js":"^3.16.0","karma":"^6.3.4","karma-chai":"^0.1.0","karma-chai-spies":"^0.1.4","karma-chrome-launcher":"^2.2.0","karma-edge-launcher":"^0.4.2","karma-firefox-launcher":"^1.1.0","karma-ie-launcher":"^1.0.0","karma-mocha":"^2.0.1","karma-mocha-reporter":"^2.2.5","karma-sauce-launcher":"^2.0.2","karma-webpack-preprocessor":"github:ekoopmans/karma-webpack-preprocessor#update-2021","mocha":"^6.1.4","pdftest":"^0.3.0","rimraf":"^2.6.2","start-server-and-test":"^1.12.0","webpack":"^5.45.1","webpack-bundle-analyzer":"^4.4.2","webpack-cli":"^4.7.2"},"scripts":{"build":"npm run clean && webpack --env=prod","build:analyze":"npm run clean && webpack --env=prod --env=analyzer","clean":"rimraf dist/*","dev":"webpack --env=dev","dev:analyze":"webpack --env=dev --env=analyzer","test":"start-server-and-test test:serve http://localhost:3000 test:run","test:serve":"pdftest serve 3000 ./test/reference/snapshot","test:run":"npx karma start karma.conf.js","release":"node ./build-scripts.js release","publish-gh":"node ./build-scripts.js publish-gh"},"_id":"@0x170818/html2pdf@1.0.0","gitHead":"2129120b590c50a46eaf7417d0e235f52b639983","_nodeVersion":"20.10.0","_npmVersion":"10.2.3","dist":{"integrity":"sha512-7nl4zObiqXeTuyO4azhKlQ9vgMMsimtqfuaJ5Ik5tw4XUxdb/DzUU0W6keWM9hUPpqFazYHx9A/AxfbKL+sj2Q==","shasum":"310dddc2efe645109e6ec496d56a4d293418d8c0","tarball":"https://registry.npmjs.org/@0x170818/html2pdf/-/html2pdf-1.0.0.tgz","fileCount":17,"unpackedSize":11772890,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIBkVpOEJu1wgge8+o3/h84GiWRxib/HvuWSusFy4tTUwAiByYrpMnDqPCAdGo2TBa+WJIGcBYCCwGw26UEhXBITYkA=="}]},"_npmUser":{"name":"0x170818","email":"0x170818@gmail.com"},"directories":{},"maintainers":[{"name":"0x170818","email":"0x170818@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/html2pdf_1.0.0_1715353840593_0.372659734382657"},"_hasShrinkwrap":false}},"time":{"created":"2024-05-10T15:10:40.495Z","1.0.0":"2024-05-10T15:10:40.880Z","modified":"2024-05-10T15:10:41.119Z"},"maintainers":[{"name":"0x170818","email":"0x170818@gmail.com"}],"description":"Client-side HTML-to-PDF rendering using pure JS","homepage":"https://ekoopmans.github.io/html2pdf.js/","keywords":["javascript","pdf-generation","html","client-side","canvas"],"repository":{"type":"git","url":"git+https://github.com/eKoopmans/html2pdf.js.git"},"author":{"name":"Erik Koopmans","email":"erik@erik-koopmans.com","url":"https://www.erik-koopmans.com"},"bugs":{"url":"https://github.com/eKoopmans/html2pdf.js/issues"},"license":"MIT","readme":"# html2pdf.js\n\nhtml2pdf.js converts any webpage or element into a printable PDF entirely client-side using [html2canvas](https://github.com/niklasvh/html2canvas) and [jsPDF](https://github.com/MrRio/jsPDF).\n\n> :warning: There have been several issues reported in v0.10. They are being investigated but in the meantime you may wish to remain on v0.9.3 (\"^0.9.3\" in npm, or [use cdnjs for HTML script tags](https://cdnjs.com/libraries/html2pdf.js/0.9.3)).\n\n## Table of contents\n\n- [Getting started](#getting-started)\n  - [CDN](#cdn)\n  - [Raw JS](#raw-js)\n  - [NPM](#npm)\n  - [Bower](#bower)\n  - [Console](#console)\n- [Usage](#usage)\n  - [Advanced usage](#advanced-usage)\n    - [Workflow](#workflow)\n    - [Worker API](#worker-api)\n- [Options](#options)\n  - [Page-breaks](#page-breaks)\n    - [Page-break settings](#page-break-settings)\n    - [Page-break modes](#page-break-modes)\n    - [Example usage](#example-usage)\n  - [Image type and quality](#image-type-and-quality)\n- [Progress tracking](#progress-tracking)\n- [Dependencies](#dependencies)\n- [Contributing](#contributing)\n  - [Issues](#issues)\n  - [Tests](#tests)\n  - [Pull requests](#pull-requests)\n- [Credits](#credits)\n- [License](#license)\n\n## Getting started\n\n#### CDN\n\nThe simplest way to use html2pdf.js is to include it as a script in your HTML by using cdnjs:\n\n```html\n<script src=\"https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js\" integrity=\"sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg==\" crossorigin=\"anonymous\" referrerpolicy=\"no-referrer\"></script>\n```\n\nUsing a CDN URL will lock you to a specific version, which should ensure stability and give you control over when to change versions. cdnjs gives you access to [all past versions of html2pdf.js](https://cdnjs.com/libraries/html2pdf.js).\n\n*Note: [Read about dependences](#dependencies) for more information about using the unbundled version `dist/html2canvas.min.js`.*\n\n#### Raw JS\n\n You may also download `dist/html2pdf.bundle.min.js` directly to your project folder and include it in your HTML with:\n\n```html\n<script src=\"html2pdf.bundle.min.js\"></script>\n```\n\n#### NPM\n\nInstall html2pdf.js and its dependencies using NPM with `npm install --save html2pdf.js` (make sure to include `.js` in the package name).\n\n*Note: You can use NPM to create your project, but html2pdf.js **will not run in Node.js**, it must be run in a browser.*\n\n#### Bower\n\nInstall html2pdf.js and its dependencies using Bower with `bower install --save html2pdf.js` (make sure to include `.js` in the package name).\n\n#### Console\n\nIf you're on a webpage that you can't modify directly and wish to use html2pdf.js to capture a screenshot, you can follow these steps:\n\n1. Open your browser's console (instructions for different browsers [here](https://webmasters.stackexchange.com/a/77337/94367)).\n2. Paste in this code:\n    ```js\n    function addScript(url) {\n        var script = document.createElement('script');\n        script.type = 'application/javascript';\n        script.src = url;\n        document.head.appendChild(script);\n    }\n    addScript('https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js');\n    ```\n3. You may now execute html2pdf.js commands directly from the console. To capture a default PDF of the entire page, use `html2pdf(document.body)`.\n\n## Usage\n\nOnce installed, html2pdf.js is ready to use. The following command will generate a PDF of `#element-to-print` and prompt the user to save the result:\n\n```js\nvar element = document.getElementById('element-to-print');\nhtml2pdf(element);\n```\n\n### Advanced usage\n\nEvery step of html2pdf.js is configurable, using its new Promise-based API. If html2pdf.js is called without arguments, it will return a `Worker` object:\n\n```js\nvar worker = html2pdf();  // Or:  var worker = new html2pdf.Worker;\n```\n\nThis worker has methods that can be chained sequentially, as each Promise resolves, and allows insertion of your own intermediate functions between steps. A prerequisite system allows you to skip over mandatory steps (like canvas creation) without any trouble:\n\n```js\n// This will implicitly create the canvas and PDF objects before saving.\nvar worker = html2pdf().from(element).save();\n```\n\n#### Workflow\n\nThe basic workflow of html2pdf.js tasks (enforced by the prereq system) is:\n\n```\n.from() -> .toContainer() -> .toCanvas() -> .toImg() -> .toPdf() -> .save()\n```\n\n#### Worker API\n\n| Method       | Arguments          | Description |\n|--------------|--------------------|-------------|\n| from         | src, type          | Sets the source (HTML string or element) for the PDF. Optional `type` specifies other sources: `'string'`, `'element'`, `'canvas'`, or `'img'`. |\n| to           | target             | Converts the source to the specified target (`'container'`, `'canvas'`, `'img'`, or `'pdf'`). Each target also has its own `toX` method that can be called directly: `toContainer()`, `toCanvas()`, `toImg()`, and `toPdf()`. |\n| output       | type, options, src | Routes to the appropriate `outputPdf` or `outputImg` method based on specified `src` (`'pdf'` (default) or `'img'`). |\n| outputPdf    | type, options      | Sends `type` and `options` to the jsPDF object's `output` method, and returns the result as a Promise (use `.then` to access). See the [jsPDF source code](https://rawgit.com/MrRio/jsPDF/master/docs/jspdf.js.html#line992) for more info. |\n| outputImg    | type, options      | Returns the specified data type for the image as a Promise (use `.then` to access). Supported types: `'img'`, `'datauristring'`/`'dataurlstring'`, and `'datauri'`/`'dataurl'`. |\n| save         | filename           | Saves the PDF object with the optional filename (creates user download prompt). |\n| set          | opt                | Sets the specified properties. See [Options](#options) below for more details. |\n| get          | key, cbk           | Returns the property specified in `key`, either as a Promise (use `.then` to access), or by calling `cbk` if provided. |\n| then         | onFulfilled, onRejected | Standard Promise method, with `this` re-bound to the Worker, and with added progress-tracking (see [Progress](#progress) below). Note that `.then` returns a `Worker`, which is a subclass of Promise. |\n| thenCore     | onFulFilled, onRejected | Standard Promise method, with `this` re-bound to the Worker (no progress-tracking). Note that `.thenCore` returns a `Worker`, which is a subclass of Promise. |\n| thenExternal | onFulfilled, onRejected | True Promise method. Using this 'exits' the Worker chain - you will not be able to continue chaining Worker methods after `.thenExternal`. |\n| catch, catchExternal | onRejected | Standard Promise method. `catchExternal` exits the Worker chain - you will not be able to continue chaining Worker methods after `.catchExternal`. |\n| error        | msg                | Throws an error in the Worker's Promise chain. |\n\nA few aliases are also provided for convenience:\n\n| Method    | Alias     |\n|-----------|-----------|\n| save      | saveAs    |\n| set       | using     |\n| output    | export    |\n| then      | run       |\n\n## Options\n\nhtml2pdf.js can be configured using an optional `opt` parameter:\n\n```js\nvar element = document.getElementById('element-to-print');\nvar opt = {\n  margin:       1,\n  filename:     'myfile.pdf',\n  image:        { type: 'jpeg', quality: 0.98 },\n  html2canvas:  { scale: 2 },\n  jsPDF:        { unit: 'in', format: 'letter', orientation: 'portrait' }\n};\n\n// New Promise-based usage:\nhtml2pdf().set(opt).from(element).save();\n\n// Old monolithic-style usage:\nhtml2pdf(element, opt);\n```\n\nThe `opt` parameter has the following optional fields:\n\n|Name        |Type            |Default                         |Description                                                                                                 |\n|------------|----------------|--------------------------------|------------------------------------------------------------------------------------------------------------|\n|margin      |number or array |`0`                             |PDF margin (in jsPDF units). Can be a single number, `[vMargin, hMargin]`, or `[top, left, bottom, right]`. |\n|filename    |string          |`'file.pdf'`                    |The default filename of the exported PDF.                                                                   |\n|pagebreak   |object          |`{mode: ['css', 'legacy']}`     |Controls the pagebreak behaviour on the page. See [Page-breaks](#page-breaks) below.                        |\n|image       |object          |`{type: 'jpeg', quality: 0.95}` |The image type and quality used to generate the PDF. See [Image type and quality](#image-type-and-quality) below.|\n|enableLinks |boolean         |`true`                          |If enabled, PDF hyperlinks are automatically added ontop of all anchor tags.                                |\n|html2canvas |object          |`{ }`                           |Configuration options sent directly to `html2canvas` ([see here](https://html2canvas.hertzen.com/configuration) for usage).|\n|jsPDF       |object          |`{ }`                           |Configuration options sent directly to `jsPDF` ([see here](http://rawgit.com/MrRio/jsPDF/master/docs/jsPDF.html) for usage).|\n\n### Page-breaks\n\nhtml2pdf.js has the ability to automatically add page-breaks to clean up your document. Page-breaks can be added by CSS styles, set on individual elements using selectors, or avoided from breaking inside all elements (`avoid-all` mode).\n\nBy default, html2pdf.js will respect most CSS [`break-before`](https://developer.mozilla.org/en-US/docs/Web/CSS/break-before), [`break-after`](https://developer.mozilla.org/en-US/docs/Web/CSS/break-after), and [`break-inside`](https://developer.mozilla.org/en-US/docs/Web/CSS/break-inside) rules, and also add page-breaks after any element with class `html2pdf__page-break` (for legacy purposes).\n\n#### Page-break settings\n\n|Setting   |Type            |Default             |Description |\n|----------|----------------|--------------------|------------|\n|mode      |string or array |`['css', 'legacy']` |The mode(s) on which to automatically add page-breaks. One or more of `'avoid-all'`, `'css'`, and `'legacy'`. |\n|before    |string or array |`[]`                |CSS selectors for which to add page-breaks before each element. Can be a specific element with an ID (`'#myID'`), all elements of a type (e.g. `'img'`), all of a class (`'.myClass'`), or even `'*'` to match every element. |\n|after     |string or array |`[]`                |Like 'before', but adds a page-break immediately after the element. |\n|avoid     |string or array |`[]`                |Like 'before', but avoids page-breaks on these elements. You can enable this feature on every element using the 'avoid-all' mode. |\n\n#### Page-break modes\n\n| Mode      | Description |\n|-----------|-------------|\n| avoid-all | Automatically adds page-breaks to avoid splitting any elements across pages. |\n| css       | Adds page-breaks according to the CSS `break-before`, `break-after`, and `break-inside` properties. Only recognizes `always/left/right` for before/after, and `avoid` for inside. |\n| legacy    | Adds page-breaks after elements with class `html2pdf__page-break`. This feature may be removed in the future. |\n\n#### Example usage\n\n```js\n// Avoid page-breaks on all elements, and add one before #page2el.\nhtml2pdf().set({\n  pagebreak: { mode: 'avoid-all', before: '#page2el' }\n});\n\n// Enable all 'modes', with no explicit elements.\nhtml2pdf().set({\n  pagebreak: { mode: ['avoid-all', 'css', 'legacy'] }\n});\n\n// No modes, only explicit elements.\nhtml2pdf().set({\n  pagebreak: { before: '.beforeClass', after: ['#after1', '#after2'], avoid: 'img' }\n});\n```\n\n### Image type and quality\n\nYou may customize the image type and quality exported from the canvas by setting the `image` option. This must be an object with the following fields:\n\n|Name        |Type            |Default                       |Description                                                                                  |\n|------------|----------------|------------------------------|---------------------------------------------------------------------------------------------|\n|type        |string          |'jpeg'                        |The image type. HTMLCanvasElement only supports 'png', 'jpeg', and 'webp' (on Chrome).       |\n|quality     |number          |0.95                          |The image quality, from 0 to 1. This setting is only used for jpeg/webp (not png).           |\n\nThese options are limited to the available settings for [HTMLCanvasElement.toDataURL()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL), which ignores quality settings for 'png' images. To enable png image compression, try using the [canvas-png-compression shim](https://github.com/ShyykoSerhiy/canvas-png-compression), which should be an in-place solution to enable png compression via the `quality` option.\n\n## Progress tracking\n\nThe Worker object returned by `html2pdf()` has a built-in progress-tracking mechanism. It will be updated to allow a progress callback that will be called with each update, however it is currently a work-in-progress.\n\n## Dependencies\n\nhtml2pdf.js depends on the external packages [html2canvas](https://github.com/niklasvh/html2canvas), [jsPDF](https://github.com/MrRio/jsPDF), and [es6-promise](https://github.com/stefanpenner/es6-promise). These dependencies are automatically loaded when using NPM or the bundled package.\n\nIf using the unbundled `dist/html2pdf.min.js` (or its un-minified version), you must also include each dependency. Order is important, otherwise html2canvas will be overridden by jsPDF's own internal implementation:\n\n```html\n<script src=\"es6-promise.auto.min.js\"></script>\n<script src=\"jspdf.min.js\"></script>\n<script src=\"html2canvas.min.js\"></script>\n<script src=\"html2pdf.min.js\"></script>\n```\n\n## Contributing\n\n### Issues\n\nWhen submitting an issue, please provide reproducible code that highlights the issue, preferably by creating a fork of [this template jsFiddle](https://jsfiddle.net/u6o6ne41/) (which has html2pdf.js already loaded). Remember that html2pdf.js uses [html2canvas](https://github.com/niklasvh/html2canvas) and [jsPDF](https://github.com/MrRio/jsPDF) as dependencies, so it's a good idea to check each of those repositories' issue trackers to see if your problem has already been addressed.\n\n#### Known issues\n\n1. **Rendering:** The rendering engine html2canvas isn't perfect (though it's pretty good!). If html2canvas isn't rendering your content correctly, I can't fix it.\n    - You can test this with something like [this fiddle](https://jsfiddle.net/eKoopmans/z1rupL4c/), to see if there's a problem in the canvas creation itself.\n\n2. **Node cloning (CSS etc):** The way html2pdf.js clones your content before sending to html2canvas is buggy. A fix is currently being developed - try out:\n    - direct file: Go to [html2pdf.js/bugfix/clone-nodes-BUILD](/eKoopmans/html2pdf.js/tree/bugfix/clone-nodes-BUILD) and replace the files in your project with the relevant files (e.g. `dist/html2pdf.bundle.js`)\n    - npm: `npm install eKoopmans/html2pdf.js#bugfix/clone-nodes-BUILD`\n    - Related project: [Bugfix: Cloned nodes](https://github.com/eKoopmans/html2pdf.js/projects/9)\n\n3. **Resizing:** Currently, html2pdf.js resizes the root element to fit onto a PDF page (causing internal content to \"reflow\").\n    - This is often desired behaviour, but not always.\n    - There are plans to add alternate behaviour (e.g. \"shrink-to-page\"), but nothing that's ready to test yet.\n    - Related project: [Feature: Single-page PDFs](https://github.com/eKoopmans/html2pdf.js/projects/1)\n\n4. **Rendered as image:** html2pdf.js renders all content into an image, then places that image into a PDF.\n    - This means text is *not selectable or searchable*, and causes large file sizes.\n    - This is currently unavoidable, however recent improvements in jsPDF mean that it may soon be possible to render straight into vector graphics.\n    - Related project: [Feature: New renderer](https://github.com/eKoopmans/html2pdf.js/projects/4)\n\n5. **Promise clashes:** html2pdf.js relies on specific Promise behaviour, and can fail when used with custom Promise libraries.\n    - In the next release, Promises will be sandboxed in html2pdf.js to remove this issue.\n    - Related project: [Bugfix: Sandboxed promises](https://github.com/eKoopmans/html2pdf.js/projects/11)\n\n6. **Maximum size:** HTML5 canvases have a [maximum height/width](https://stackoverflow.com/a/11585939/4080966). Anything larger will fail to render.\n    - This is a limitation of HTML5 itself, and results in large PDFs rendering completely blank in html2pdf.js.\n    - The jsPDF canvas renderer (mentioned in Known Issue #4) may be able to fix this issue!\n    - Related project: [Bugfix: Maximum canvas size](https://github.com/eKoopmans/html2pdf.js/projects/5)\n\n### Tests\n\nhtml2pdf.js is currently sorely lacking in unit tests. Any contributions or suggestions of automated (or manual) tests are welcome. This is high on the to-do list for this project.\n\n### Pull requests\n\nIf you want to create a new feature or bugfix, please feel free to fork and submit a pull request! Create a fork, branch off of `master`, and make changes to the `/src/` files (rather than directly to `/dist/`). You can test your changes by rebuilding with `npm run build`.\n\n## Credits\n\n[Erik Koopmans](https://github.com/eKoopmans)\n\n#### Contributors\n\n- [@WilcoBreedt](https://github.com/WilcoBreedt)\n- [@Ranger1230](https://github.com/Ranger1230)\n\n#### Special thanks\n\n- [Sauce Labs](https://saucelabs.com/) for unit testing.\n\n## License\n\n[The MIT License](http://opensource.org/licenses/MIT)\n\nCopyright (c) 2017-2019 Erik Koopmans <[http://www.erik-koopmans.com/](http://www.erik-koopmans.com/)>\n","readmeFilename":"README.md"}