-
Notifications
You must be signed in to change notification settings - Fork 142
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
Write images to PDF as soon as possible (otherwise easy to run out of memory) #291
Comments
The issue is probably that the rendering does not progress until you've read all images into memory. A workaround might be the following: for (const image in new Array(30).fill(1)) {
const img = new pdf.Image(
await sharp(`images/${files[image]}`)
.resize({ width: 2217, height: 2217, fit: 'inside' })
.toBuffer()
);
doc.image(img);
+ await doc._next();
} This should write each image to the PDF before continuing with the next image. However, this is an internal API, so in case it works, I'd keep the issue open until there is a public API to achieve that. |
Hi, already tried this. |
Are you also running out of memory if you skip the pdfjs related stuff from for (const image in new Array(30).fill(1)) {
- const img = new pdf.Image(
await sharp(`images/${files[image]}`)
.resize({ width: 2217, height: 2217, fit: 'inside' })
.toBuffer()
- );
- doc.image(img);
} Just to make sure it is pdfjs related and not sharp related? |
After running this file: import { createWriteStream, readdirSync, readFileSync } from 'fs';
import * as pdf from 'pdfjs';
const doc = new pdf.Document({
font: require('pdfjs/font/Helvetica'),
width: 9 * 72,
height: 6 * 72,
});
const writeStream = createWriteStream('output.pdf', {});
doc.pipe(writeStream);
const files = readdirSync('D:/small');
const handleFiles = async () => {
for (const image in new Array(3000).fill(1)) {
console.log(files[image]);
const img = new pdf.Image(readFileSync(`D:/small/${files[image]}`));
doc.image(img);
}
};
handleFiles().then(() => {
console.log('finish');
doc.end();
});` I'm ending up with out of memory and output.pdf with size of 145M |
Hi! do you have any ideas about what might be the reason for this? Happy New Year :) |
Thanks for the example above. I gave it a more detailed look, and it is |
Thanks! Could you point me to a place where I should dig into pdfjs? Maybe I can help with this. |
The image data is written into the file here: https://github.com/rkusa/pdfjs/blob/main/lib/document.js#L563 |
Hi!
I'm having a strange issue with a large number of images.
Looks like streaming waits for .end() to push all data to the file.
As a result, I'm getting out of memory. output.pdf is empty, for the whole time before .end() is called.
What I'm missing?
The text was updated successfully, but these errors were encountered: