PejmanNik / puppeteer-report

Convert HTML to PDF by Puppeteer with support of adding a custom header, footer, and page number
MIT License
148 stars 28 forks source link

Return Buffer instead of bytes #36

Closed JoaquimLey closed 2 years ago

JoaquimLey commented 3 years ago

since -report is using pdf-lib the return value from the .page and .pdf is Promise<Uint8Array> which in turn is giving us a lot of issues when combing with node-signpdf that we are using to sign the PDF.

We're using puppeteer-report due to the limitations of puppeteer regarding running scripts on the footer/header templates (thank you for this lib btw) but now having trouble combining the two.

Any pointers on how we could use them or are you able to open a method/option to return the Buffer?

Current implementation

// --------------------------------- The page variable Is the puppeteer page with the HTML
const pdfBytes = await report.pdfPage(page, { format: this.config.format(), margin: this.config.margin() });
const placeholderPdfBuffer = plainAddPlaceholder({
        pdfBuffer: Buffer.from(pdfBytes),
        reason: reason,
        name: name,
        contactInfo: contactInfo,
});
return signer.sign(placeholderPdfBuffer, certBuffer);

And running plainAddPlaceholder throws an exception:

TypeError: Cannot read property 'split' of undefined
    at getXref (.../node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readRefTable.js:55:34)

Original: https://github.com/vbuch/node-signpdf/blob/develop/src/helpers/plainAddPlaceholder/readRefTable.js#L48

Any pointers would be greatly appreciated and thank you in advance for your time.

PejmanNik commented 3 years ago

Does it work with the puppeteer pdf function? if yes, please create a simple repo that replicates this issue, so I can debug the output.

JoaquimLey commented 3 years ago

It does, it simply uses the bytes returning the .page and running the same method from node-sign-pdf. It outputs that error (that's literally that) since the -report returns a Uint8Array and not the Buffer.

JoaquimLey commented 2 years ago

@PejmanNik sorry for the delay wasn't able to get online this week. Was finally able to create a small example:

Run with

node app.js
JoaquimLey commented 2 years ago

@PejmanNik any updates?

PejmanNik commented 2 years ago

I was on vacation and couldn't find the time to debug. Did the mentioned PR will resolve your issue?

JoaquimLey commented 2 years ago

Yes.

On Sun, 21 Nov 2021, 13:16 Pejman, @.***> wrote:

I was on vacation and couldn't find the time to debug. Did the mentioned PR will resolve your issue?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PejmanNik/puppeteer-report/issues/36#issuecomment-974815941, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABU2D7F23VT64TJQZ3YFXULUNDWJBANCNFSM5G7Z6TBA .

PejmanNik commented 2 years ago

ok, I will close this issue, the PR needs one minor change and I can release the new version as soon as @miguelangelomendes fix it.