Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance poor in webworker using data uri pngs. #1102

Open
jsoo1 opened this issue Apr 9, 2020 · 2 comments
Open

Performance poor in webworker using data uri pngs. #1102

jsoo1 opened this issue Apr 9, 2020 · 2 comments

Comments

@jsoo1
Copy link

jsoo1 commented Apr 9, 2020

Bug Report

I found #855, #264, #269, and #1081. All of which seem related to the problem I am having.

Description of the problem

I want to make a pdf out of full page pngs which are data URIs. I want to run in a webworker and have compression so I cannot say which of the afforementioned issues is really to blame.

The problem is that performance is very bad. The problem seems to scale with number of pages. One page is "fast" (~1.5 seconds). Two pages is much slower. I recorded several data points (as many as patience granted).

These data were generated using the code below.

pages time
1 1561.501953125ms
1 1581.448974609375ms
1 1658.505126953125ms
2 175932.03173828125ms
2 173780.81176757812ms
2 178272.82006835938ms
3 235536.85009765625ms
3 234518.7412109375ms
3 233301.33813476562ms
4 237735.68701171875ms

Code sample

const PDFDocument = require('pdfkit/js/pdfkit.standalone.js')
const blobStream  = require('blob-stream')

onmessage = ({ data }) => {
    const dpiScale = 72 / 300
    let pdf = new PDFDocument({ autoFirstPage: false })
    let stream = pdf.pipe(blobStream())
    data.forEach(c => {
        pdf.addPage({
            size: "letter",
        }).image(c.dataurl, 0, 0, {
            width: c.size.width * dpiScale,
            height: c.size.height * dpiScale
        })
        delete pdf._imageRegistry[c.dataurl] // <- Got this from #1081
    })
    console.time('data')
    pdf.end()
    stream.on('finish', () => {
        console.timeEnd('data')
        let blob = stream.toBlob('application/pdf')
        postMessage(blob)
    })
}

Your environment

  • pdfkit version: 0.10.0
  • Node version: N/A
  • Browser version (if applicable): Google Chrome Version 80.0.3987.149 (Official Build) (64-bit)
  • Operating System: macOS Catalina 10.15.4
@jsoo1
Copy link
Author

jsoo1 commented Apr 9, 2020

When removed from the webworker, times reduced greatly.

new Promise((resolve, reject) => {
    try {
        const dpiScale = 72 / 300
        let pdf = new PDFDocument({ autoFirstPage: false })
        let stream = pdf.pipe(blobStream())
        conversions.forEach(c => {
            pdf.addPage({
                size: "letter",
                layout: c.size.width > c.size.height ? "landscape" : "portrait"
            }).image(c.dataurl, 0, 0, {
                width: c.size.width * dpiScale,
                height: c.size.height * dpiScale
            })
            delete pdf._imageRegistry[c.dataurl]
        })
        console.time('pdf')
        pdf.end()
        stream.on('finish', () => {
            console.timeEnd('pdf')
            resolve(stream.toBlob('application/pdf'))
        })
    } catch (e) {
        reject(e)
    }
})
pages time
1 1488.033203125ms
1 1233.98486328125ms
1 1121.4951171875ms
2 2580.294189453125ms
2 2574.9169921875ms
2 2376.490966796875ms
3 3750.450927734375ms
3 3778.68798828125ms
3 3539.64892578125ms
4 5161.190185546875ms
4 4778.90380859375ms
4 6695.65380859375ms

@jsoo1
Copy link
Author

jsoo1 commented Apr 9, 2020

The same performance happens with pdfkit version 0.11.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant