Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.89k stars 656 forks source link

Error on flattening with some pdf #1519

Open caeldev1 opened 1 year ago

caeldev1 commented 1 year ago

What were you trying to do?

I am trying to flatten form but got error:

const URL = await blobToURL(files[0]); const pdfDoc = await PDFDOcument.load(URL, {ignoreEncryption:true}); const form = pdfDoc.getForm(); form.flatten()); //... Expected instance of PDFDict2, but got instance of undefined ... at PDFForm2.flatten (PDFForm.ts:539:12)

The PDF: every PDF in this website: https://catalogue.servicecanada.gc.ca/content/EForms/en/Detail.html?Form=EMP5611 eg: https://catalogue.servicecanada.gc.ca/content/EForms/en/CallForm.html?Lang=en&PDF=ESDC-EMP5611.pdf

In any normal pdf, it's working

How did you attempt to do it?

What I want to do is to flatten then view it but there is this error

const URL = await blobToURL(files[0]); const pdfDoc = await PDFDOcument.load(URL, {ignoreEncryption:true}); const form = pdfDoc.getForm(); form.flatten()); //...

What actually happened?

there is an error: Expected instance of PDFDict2, but got instance of undefined ... at PDFForm2.flatten (PDFForm.ts:539:12)

What did you expect to happen?

Is there any workaround I didn't know, why this is not working with this kind of pdf?

How can we reproduce the issue?

` import React, { useCallback } from "react"; import { useDropzone } from "react-dropzone";

    <Drop
      onLoaded={async (files) => {
        const URL = await blobToURL(files[0]);
        const pdfDoc = await PDFDocument.load(URL, { ignoreEncryption: true });
        const form = pdfDoc.getForm();
          form.flatten();
          const pdfBy = await pdfDoc.save();
          const blob = new Blob([new Uint8Array(pdfBy)]);
          const URL2 = await blobToURL(blob);
         // setPdf(URL2);
         // dispatch({ type: GABARIT_ACTION.PDF_MODULE.CHANGE_INIT_PDF, payload: URL2 });
      }}
    />

    function Drop({ onLoaded }) {

      const onDrop = useCallback((acceptedFiles) => {
        onLoaded(acceptedFiles);
      }, []);
      const { getRootProps, getInputProps, isDragActive } = useDropzone({
        onDrop,
        accept: {"application/pdf":['.pdf']},
      });

      return (
        <div {...getRootProps()}>
          <input {...getInputProps()} />
          {isDragActive ? <p>Drop a PDF here</p> : <p>Uploader le document</p>}
        </div>
      );
    }

`

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response