RelaxedJS / ReLaXed

Create PDF documents using web technologies
ISC License
11.8k stars 428 forks source link

Possible to get access to pdf bytestream? #98

Open thedewpoint opened 6 years ago

thedewpoint commented 6 years ago

I'm going to be leveraging relaxedjs through a microservice, If possible I would like to avoid having to save a pdf file, serve it up, and then delete it. If relaxed could also return the byestream so that I could serve it from a URL, that would be great.

EDIT: just for some context I found that there is a <Promise> method here https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions

Zulko commented 6 years ago

Ok. Quick question though: what is your use case and why not use puppeteer directly ?

thedewpoint commented 6 years ago

@Zulko My goal was to store the pug design file on my rest service, and when requests were made providing certain data, relaxed would template that data across my pug design file and return the pdf buffer for me to pipe back in the response. Would puppeteer be better suited for that?

Zulko commented 6 years ago

Yes, the puppeteer code is very simple and will have the advantage that you won't start a new chromium instance at each request. See that approximative snippet below (you will have to google a bit, I am not sure about the exact names of the methods):

// create a Chromium "page" once at server start:
const browser = await puppeteer.launch();
const page = await browser.newPage();

// in your request handler:
var html = pug.parse("my_template.pug", request.parameters)
await page.setHTML(html, {waitUntil: 'networkidle2'});
var pdfBuffer = await page.pdf({format: 'A4'});

ReLaXed provides some advantages like plugins for mathjax support and other goodies, but most features are oriented for interactive use.

thedewpoint commented 6 years ago

@Zulko thank you! Sounds good