-
Notifications
You must be signed in to change notification settings - Fork 23
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
PDFDocument.saveToBuffer with options not working #100
Comments
As a follow up, would it be possible to compress the pdf slide in a different process before converting to an image? Either in one of the following steps
|
First off I wonder why you're trying to "compress the pdf before converting it to images", and what you hope to accomplish with that? Saving the PDF is intended to save a PDF document (with any edits) to file. The various save options only affect the byte representation of the PDF on disk. Opening and instantly saving a PDF file will change the bytes on on disk, but should result in an identical appearance and behavior when opening the new file. For example the "compress-images" option will make sure that uncompressed images are compressed with Flate. It will not change or re-compressed any JPEG images if that is what you're trying to do! Likewise, the "garbage" option will remove unused objects from the file. If you're just saving out a file without having done any edits, there is nothing to garbage collect. -- The options is a comma separated string of properties (converting an object into this string is a TODO item, so future versions may support option 1 but for now you'll need to use option 3 or 4). If a property value is present but not set to a value, it is implied to be "yes". Only variations 3 and 4 should work.
The error you get is because you try to save with linearization to a Buffer. The linearization code is not very well tested, and only works on "seekable" outputs (which Buffer is not). In any case I would I would recommend NOT using the linearize option, as its only real effect is in making the resulting PDF bigger and slower. |
Thanks for your feedback. The issue is that when I'm converting a image-heavy page to an image the mupdf-wasm package doesn't always work. However, the reason was that I scale the So compressing the PDF before converting it with My temp fix is to just increase the matrix to 2x depending on the existing resolution of the pdf page. It's an ok workaround. |
"Compressing" the PDF before converting won't necessarily help. What exact error message did you see when converting the image-heavy page? Did it run out of memory? |
Yes it ran out of memory at 3x matrix scale but worked well at 2x. |
Hi there,
I want to add
mutool convert options
to a PDFDocument buffer to compress the pdf before converting it to images.I found in the docs that I can pass these options to
PDFDocument.saveToBuffer
However, the example in the docs doesn't match with the actual implementation.
Docs Example:
However, the implementation shows that options should be a string not an object: https://github.com/ArtifexSoftware/mupdf.js/blob/68a506ad218c6f439169ebd07ddc33e150c22601/src/mupdf.ts#L2281C2-L2285C2
Ok so I pass it as a string, however, I'm not sure what format it should be, so I did a little further digging into
libmupdf
and found the parsing function for buffer write options: https://github.com/ArtifexSoftware/mupdf/blob/53aae51af4eea14fabde144948ba61f7537053f9/source/pdf/pdf-write.c#L3264Which one is the correct way to pass options to
saveToBuffer
in mupdf-wasm?:var buffer = pdfDocument.saveToBuffer({"compress-images":true, "linearize":true, "garbage":true});
var buffer = pdfDocument.saveToBuffer("compress-images:true,linearize:true,garbage:true");
var buffer = pdfDocument.saveToBuffer("compress-images=yes,linearize=yes,garbage=yes");
var buffer = pdfDocument.saveToBuffer("compress-images,linearize,garbage");
Only 2. variation works but I have doubts if the options are parsed properly
Every other variation throws this error
Would really appreciate your help / some pointers.
The text was updated successfully, but these errors were encountered: