pdf-lib icon indicating copy to clipboard operation
pdf-lib copied to clipboard

Failed to parse PDF document (line:2 col:630 offset=354): No PDF header found

Open devluked opened this issue 4 years ago • 4 comments

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

devluked avatar Nov 29 '21 17:11 devluked

I also got this error, looks like I'm doing everything right according to the pdf-lib docs and yet...errors.

cgrady3 avatar Dec 02 '21 18:12 cgrady3

@cgrady3 Sigh yeah don't know why this is happening, driving me nuts. Btw is your picture in Bryce Canyon?

devluked avatar Dec 08 '21 16:12 devluked

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!

cgrady3 avatar Dec 08 '21 18:12 cgrady3

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/

Gcarter17 avatar Mar 08 '22 09:03 Gcarter17

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);
}

jerry-flam avatar Oct 16 '22 15:10 jerry-flam