Failed to parse PDF document (line:2 col:630 offset=354): No PDF header found
What were you trying to do?
Trying to download the PDF
How did you attempt to do it?
const promise = new Promise(async (resolve, reject) => {
this.setState({ disabled: true });
const { downloadURL } = this.props;
fileUrl = await downloadURL(doc.media_file_id);
if (fileUrl) resolve(fileUrl);
});
//await file url
promise.then(async () => {
//fetch PDF data and load into variable
const existingPdfBytes = await fetch(fileUrl.url).then((res) =>
res.arrayBuffer()
);
const pdfDoc = await PDFDocument.load(existingPdfBytes, {
ignoreEncryption: true,
});
This line is causing the code to break:
const pdfDoc = await PDFDocument.load(existingPdfBytes, {
ignoreEncryption: true,
});
What actually happened?
i get the warning about the header missing
What did you expect to happen?
the document to download
How can we reproduce the issue?
const promise = new Promise(async (resolve, reject) => {
this.setState({ disabled: true });
const { downloadURL } = this.props;
fileUrl = await downloadURL(doc.media_file_id);
if (fileUrl) resolve(fileUrl);
});
//await file url
promise.then(async () => {
//fetch PDF data and load into variable
const existingPdfBytes = await fetch(fileUrl.url).then((res) =>
res.arrayBuffer()
);
const pdfDoc = await PDFDocument.load(existingPdfBytes, {
ignoreEncryption: true,
});
const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
Line where the code crashes:
const pdfDoc = await PDFDocument.load(existingPdfBytes, {
ignoreEncryption: true,
});
Version
1.16
What environment are you running pdf-lib in?
Browser
Checklist
- [X] My report includes a Short, Self Contained, Correct (Compilable) Example.
- [X] I have attached all PDFs, images, and other files needed to run my SSCCE.
Additional Notes
No response
I also got this error, looks like I'm doing everything right according to the pdf-lib docs and yet...errors.
@cgrady3 Sigh yeah don't know why this is happening, driving me nuts. Btw is your picture in Bryce Canyon?
My teammate found that we didn't run into the issue if we fed it in as base64 instead of uint8.
Antelope canyon but good eye!
Found out this was actually a CORS issue, first time pulling down a file from my storage bucket and all. Setup CORS and works just fine now. If you're using firebase check out this link https://jefrydco.id/en/blog/fix-cors-issue-firebase-google-cloud/
I have this issue with v1.17.1 and I worked around it by converting the file to PDF/A using ghostscript.
My code is like this:
const { PDFDocument } = require('pdf-lib');
const fs = require('fs');
const { execSync } = require('child_process');
try {
await PDFDocument.load(fs.readFileSync(`./pdfs/file.pdf`), {
ignoreEncryption: true,
throwOnInvalidObject: true,
});
} catch (error) {
const command =
`gs -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -dSAFER ` +
`-sColorConversionStrategy=UseDeviceIndependentColor -dEmbedAllFonts=true ` +
`-dPrinted=true -dPDFA -sProcessColorModel=DeviceRGB -dPDFACompatibilityPolicy=1 ` +
`-dDetectDuplicateImages -r210 -dFastWebView=true ` +
`-sOutputFile=./pdfs/file_fixed.pdf ./pdfs/file.pdf`;
execSync(command);
}