UNPKG

@coolgk/pdf

Version:

html to PDF module. create PDF files from html string or file.

69 lines (58 loc) 1.76 kB
## @coolgk/pdf a javascript / typescript module `npm install @coolgk/pdf` html to PDF module. create PDF files from html string or file. Report bugs here: [https://github.com/coolgk/node-utils/issues](https://github.com/coolgk/node-utils/issues) ## Examples ```javascript // for "error while loading shared libraries: libfontconfig.so" run "sudo apt-get -y install libfontconfig" import { Pdf, Format, Orientation } from '@coolgk/pdf'; // OR // const { Pdf, Format, Orientation } = require('@coolgk/pdf'); const pdf = new Pdf({ tmpConfig: { dir: '/tmp/pdf' } // optional }); pdf.createFromHtmlFile( '/tmp/test.html', { header: { height: '1cm', contents: "<strong style='color: red'>Page ${pageNumber} of ${numberOfPages} - ${pageNumber}</strong>" }, footer: { height: '1cm', contents: 'footer <strong>Page ${pageNumber} of ${numberOfPages}</strong>' }, margin: '0.5cm' } ).then((pdfFile) => { console.log(pdfFile); }); const htmlCode = `<!DOCTYPE html><html><head> <title>CCES</title> <style> .pagebreak { page-break-after: always; } h2, h1 { color: red } </style> </head> <body> <div> <h1>page 1</h1> <p>some text <img src='https://dummyimage.com/600x400/3bbda9/f516ae.jpg'></p> </div> <div class="pagebreak"></div> <div> <h2>page 2</h2> <table> <tr> <td>texgt</td> <td>text</td> </tr> </table> </div> </body> </html>`; pdf.createFromHtmlString(htmlCode).then((pdfFile) => { console.log(pdfFile); }); ```