10 examples of 'react-native-html-to-pdf' in JavaScript

Every line of 'react-native-html-to-pdf' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
47async function createPDF(html) {
48 let browser: any = null;
49 try {
50 browser = await getPuppeteer();
51 const page = await browser.newPage();
52 await page.setContent(html);
53 return await page.pdf(pdfOptions);
54 } catch (error) {
55 throw error;
56 } finally {
57 if (browser) {
58 await browser.close();
59 }
60 }
61}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

59function openPDF({ patient, bgUnits = MGDL_UNITS }) {
60 const doc = new PDFDocument({ autoFirstPage: false, bufferPages: true, margin: MARGIN });
61 const stream = doc.pipe(blobStream());
62 const opts = {
63 bgPrefs: {
64 bgBounds: bgBounds[bgUnits],
65 bgUnits,
66 },
67 timePrefs: {
68 timezoneAware: true,
69 timezoneName: 'US/Eastern',
70 },
71 patient,
72 };
73
74 createPrintView('basics', data[bgUnits].basics, opts, doc).render();
75 PrintView.renderPageNumbers(doc);
76
77 doc.end();
78
79 stream.on('finish', () => {
80 window.open(stream.toBlobURL('application/pdf'));
81 });
82}
58function openPDF({ patient, bgUnits = MGDL_UNITS }) {
59 const doc = new PDFDocument({ autoFirstPage: false, bufferPages: true, margin: MARGIN });
60 const stream = doc.pipe(blobStream());
61 const opts = {
62 bgPrefs: {
63 bgBounds: bgBounds[bgUnits],
64 bgUnits,
65 },
66 timePrefs: {
67 timezoneAware: true,
68 timezoneName: 'US/Eastern',
69 },
70 numDays: {
71 bgLog: 30,
72 },
73 patient,
74 };
75
76 createPrintView('bgLog', data[bgUnits].bgLog, opts, doc).render();
77 PrintView.renderPageNumbers(doc);
78
79 doc.end();
80
81 stream.on('finish', () => {
82 window.open(stream.toBlobURL('application/pdf'));
83 });
84}
16toPdf() {
17 const renderOptions = {
18 completionTrigger: this.completionTrigger(),
19 printOptions: this.printOptions(),
20 host: 'localhost',
21 port: 9222
22 }
23
24 this.log(`Generating PDF for HTML string with options: ${JSON.stringify(renderOptions)}`)
25
26 return htmlPdf.create(this.htmlString, renderOptions)
27}
111async renderPdf(url, options) {
112 return new Promise((resolve, reject) => {
113 CDP({host: this.host, port: this.port}, async (client) => {
114 try{
115 this.log(`Opening ${url}`);
116 const {Page, Emulation, LayerTree} = client;
117 await Page.enable();
118 await LayerTree.enable();
119
120 const loaded = this.cbToPromise(Page.loadEventFired);
121 const jsDone = this.cbToPromise(Emulation.virtualTimeBudgetExpired);
122
123 await Page.navigate({url});
124 await Emulation.setVirtualTimePolicy({policy: 'pauseIfNetworkFetchesPending', budget: 5000});
125
126 await this.profileScope('Wait for load', async () => {
127 await loaded;
128 });
129
130 await this.profileScope('Wait for js execution', async () => {
131 await jsDone;
132 });
133
134 await this.profileScope('Wait for animations', async () => {
135 await new Promise((resolve) => {
136 setTimeout(resolve, 5000); // max waiting time
137 let timeout = setTimeout(resolve, 100);
138 LayerTree.layerPainted(() => {
139 clearTimeout(timeout);
140 timeout = setTimeout(resolve, 100);
141 });
142 });
143 });
144
145 const pdf = await Page.printToPDF(options);
146 const buff = Buffer.from(pdf.data, 'base64');
147 client.close();
148 resolve(buff);
149 }catch (e) {
150 reject(e.message)
151 }
152 });
153 });
154}
10function makePDF(PDFDocument, blobStream, lorem, iframe) {
11 // create a document and pipe to a blob
12 var doc = new PDFDocument();
13 var stream = doc.pipe(blobStream());
14
15 // draw some text
16 doc.fontSize(25).text('Here is some vector graphics...', 100, 80);
17
18 // some vector graphics
19 doc
20 .save()
21 .moveTo(100, 150)
22 .lineTo(100, 250)
23 .lineTo(200, 250)
24 .fill('#FF3300');
25
26 doc.circle(280, 200, 50).fill('#6600FF');
27
28 // an SVG path
29 doc
30 .scale(0.6)
31 .translate(470, 130)
32 .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
33 .fill('red', 'even-odd')
34 .restore();
35
36 // and some justified text wrapped into columns
37 doc
38 .text('And here is some wrapped text...', 100, 300)
39 .font('Times-Roman', 13)
40 .moveDown()
41 .text(lorem, {
42 width: 412,
43 align: 'justify',
44 indent: 30,
45 columns: 2,
46 height: 300,
47 ellipsis: true
48 });
49
50 // end and display the document in the iframe to the right
51 doc.end();
52 stream.on('finish', function() {
53 iframe.src = stream.toBlobURL('application/pdf');
54 });
55}
4module.exports = function buildPdf(input, output) {
5 const html = fs.readFileSync(input, 'utf8');
6 const options = {
7 format: 'A4',
8 orientation: 'portrait',
9 border: '2.54cm',
10 };
11
12 return new Promise((resolve, reject) => {
13 pdf.create(html, options).toFile(output, function (err) {
14 if (err) {
15 console.log(err);
16 reject(err);
17 }
18 resolve();
19 });
20 })
21}
45function printPDF(Page) {
46 console.log("printing!")
47 return new Promise(resolve =>
48 Page.printToPDF({
49 printBackground: true,
50 marginTop: 0.25,
51 marginBottom: 0.15,
52 marginRight: 0.15,
53 marginLeft: 0.15,
54 paperWidth: 8.3,
55 paperHeight: 11.7
56 }).then(resolve)
57 )
58}
62function pdfRenderer(options, done) {
63 options.output = "pdf";
64 templateRenderer("default.ejs", options, function(err, html) {
65 if (err) return done(new Error("Error parsing ejs: " + err));
66 //if(options.minify) html=htmlMinifier(html);
67 html2pdf(html, options.fileName, done);
68 });
69}
32function setPDF(blob) {
33 document.querySelector('#blob').src = URL.createObjectURL(blob);
34}

Related snippets