ReLaXed icon indicating copy to clipboard operation
ReLaXed copied to clipboard

Possible to get access to pdf bytestream?

Open thedewpoint opened this issue 6 years ago • 4 comments

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<Buffer>> method here https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions

thedewpoint avatar Jun 26 '18 17:06 thedewpoint

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

Zulko avatar Jun 26 '18 17:06 Zulko

@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?

thedewpoint avatar Jun 26 '18 17:06 thedewpoint

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.

Zulko avatar Jun 26 '18 17:06 Zulko

@Zulko thank you! Sounds good

thedewpoint avatar Jun 26 '18 17:06 thedewpoint