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
"Uncaught (in promise) Error: Invalid Stream!" #12
Comments
I was able to reproduce this parsing error using the |
I found the cause of the issue and will publish an update containing a fix. The // Locate the end of the stream
const endstreamIdx =
arrayIndexOf(trimmed, '\nendstream') ||
arrayIndexOf(trimmed, '\rendstream');
if (!endstreamIdx && endstreamIdx !== 0) error('Invalid Stream!'); Adding another keyword string results in a successful parsing of the document: // Locate the end of the stream
const endstreamIdx =
arrayIndexOf(trimmed, '\nendstream') ||
arrayIndexOf(trimmed, '\rendstream') ||
arrayIndexOf(trimmed, 'endstream');
if (!endstreamIdx && endstreamIdx !== 0) error('Invalid Stream!'); It's not actually uncommon for PDFs in the wild to fail to conform exactly to the specification, but that doesn't mean Here is the section of the
Manually editing the PDF file in a text editor and adding a newline as follows also fixes the problem:
Also, if you're interested, here's the relevant section of the PDF 1.7 Specification which specifies that a
|
@vitaly-zdanevich I've just published version This new method essentially resets the graphics state of a PDFPage to its default state. This can be useful if you're modifying a PDF that modifies the graphics state and doesn't clean up its changes. I noticed that this was the case with the import { PDFDocumentFactory, PDFDocumentWriter, drawText } from 'pdf-lib';
const pdfDoc = PDFDocumentFactory.load(existingPdfDocBytes);
const [helveticaFont] = pdfDoc.embedStandardFont('Helvetica');
const pages = pdfDoc.getPages();
const page = pages[0];
page
.addFontDictionary('Helvetica', helveticaFont)
.normalizeCTM(pdfDoc);
const contentStream = pdfDoc.createContentStream(
drawText('This text was added to the PDF with JavaScript!', {
x: 25,
y: 25,
size: 24,
font: 'Helvetica',
colorRgb: [0.95, 0.26, 0.21],
}),
);
page.addContentStreams(pdfDoc.register(contentStream));
const pdfBytes = PDFDocumentWriter.saveToBytes(pdfDoc); You can install this new version with npm:
It's also available on unpkg: |
I do want to mention that I don't think it's desirable to have to manually call |
Thank you, now PDF loaded without errors. |
@vitaly-zdanevich Do you mind if I add the |
Version You can install this new version with npm:
It's also available on unpkg: |
Yes you can use this PDF, this is from Stripe - popular payment gateway. |
My code:
receipt.pdf
The text was updated successfully, but these errors were encountered: