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

Error while trying to add page from a copy

Open SamuelLANDIS opened this issue 2 years ago • 3 comments

What were you trying to do?

I was trying to add a page in a new PDF Document from another file

How did you attempt to do it?

Below is all my code, with comments My file is "gestionPDF.mjs", ran with "nodemon" /node.js v19.3.0

/*
fichier dédié aux PDF
*/

import { degrees, PDFDocument, rgb, StandardFonts } from 'pdf-lib';
import * as fs from 'fs';


export async function fabriquePDF() {
    // lecture du template
    const template = 'PDF/TemplateBordereau.pdf';
    const contenuTemplate = fs.readFileSync(template);
    const T=await PDFDocument.load(contenuTemplate);
    const first=T.getPage(0);     // I added this to try to fix the Pb
    const { width, height } = first.getSize()       // also this

    // creation du nouveau document
    const pdfDoc = await PDFDocument.create();

    // copy du template en guise de fond
    const fond=await pdfDoc.copyPages(T,[0]);

    console.log(fond)       // OK
    console.log([width,height])     // OK : Known ([540,780])
    

    // ajout de la page
    pdfDoc.addPage(fond);           // Not Working (see just below)



    const pdfBytes = await pdfDoc.save()
    fs.writeFileSync("PDF/BIDON.PDF", pdfBytes);
}


fabriquePDF();

TemplateBordereau.pdf

What actually happened?

F:\SN100PC\Developpements\CPC_Back\node_modules\pdf-lib\cjs\utils\validators.js:140 throw new TypeError(exports.createTypeErrorMsg(value, valueName, types)); ^

TypeError: width must be of type number, but was actually of type NaN at exports.assertIs (F:\SN100PC\Developpements\CPC_Back\node_modules\pdf-lib\cjs\utils\validators.js:140:11) at PDFPage.setSize (F:\SN100PC\Developpements\CPC_Back\node_modules\pdf-lib\cjs\api\PDFPage.js:100:17) at PDFDocument.insertPage (F:\SN100PC\Developpements\CPC_Back\node_modules\pdf-lib\cjs\api\PDFDocument.js:606:26) at PDFDocument.addPage (F:\SN100PC\Developpements\CPC_Back\node_modules\pdf-lib\cjs\api\PDFDocument.js:565:21) at fabriquePDF (file:///F:/SN100PC/Developpements/CPC_Back/gestionPDF.mjs:28:12) */

What did you expect to happen?

BIDON.PDF

How can we reproduce the issue?

I think you have all elements . If not : please mail me : samuel.landis.btlc -at- gmail.com

Version

1.17.1

What environment are you running pdf-lib in?

Node

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

Thank You !!!

SamuelLANDIS avatar Feb 04 '23 04:02 SamuelLANDIS

In my case , I wanted to add from 1 pdf to another a page

try {
     pdfDoc.insertPage(0, existingPage);
 } catch (error) {
    console.log(error);
 }
Screenshot 2023-05-25 at 16 59 48

KonvisarovAlexandr avatar May 25 '23 14:05 KonvisarovAlexandr

This error occurs because you're passing something that isn't a page. In my case, it was an array of pages.

avkliskova avatar Nov 30 '23 18:11 avkliskova

Any updates?

davevilela avatar Jul 23 '24 17:07 davevilela