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

File Size increasing after modifying.

Open siriv1997 opened this issue 3 years ago • 0 comments

What were you trying to do?

  1. I have 12 pages pdf with 27 MB.
  2. I am going to extract 3 pages from that, draw square boxes on that pages, and creating a new pdf.
  3. Everything is fine up to that but once after pdf is created the file size is increasing to 83 MB.

How did you attempt to do it?

I am pasting the approach that I am following: Code Snippet:

const pdfDoc=await PDFDocument.load(bufferData); //Original PDF buffer Data
const mergedPdf=await PDFDocument.create(); //Creating new PDF
let pages = [1,11,13]; //Pages
let geometrywithPages={ '1':[ { geometry:
        '{\'Width\': 0.028202150017023087, \'Height\': 0.008906005881726742, \'Left\': 0.2978954613208771, \'Top\': 0.9221436977386475}',
       page: 1,' },
     { geometry:
        '{\'Width\': 0.028140360489487648, \'Height\': 0.008644775487482548, \'Left\': 0.2948639988899231, \'Top\': 0.7778991460800171}',
       page: 1,} ],
  '11':
   [ { geometry:
        '{\'Width\': 0.02779320813715458, \'Height\': 0.008917317725718021, \'Left\': 0.3038844168186188, \'Top\': 0.9173278212547302}',
       page: 11,},
  ],
  '13':
   [ { geometry:
        '{\'Width\': 0.0268331877887249, \'Height\': 0.008892822079360485, \'Left\': 0.31267306208610535, \'Top\': 0.8992018103599548}',
       page: 13 },
     { geometry:
        '{\'Width\': 0.15413451194763184, \'Height\': 0.04401253163814545, \'Left\': 0.5219976902008057, \'Top\': 0.8711878061294556}',
       page: 13} ] }

for (let i=0; i<pages.length; i++) {
    let pageGeometry=geometrywithPages[pages[i]];
          const firstPage='';
          for (let k=0; k<pageGeometry.length; k++) {
              let geometry=pageGeometry[k].geometry;
              let searchGeometry=JSON.parse(JSON.stringify(eval("("+geometry+")")));
              let B_W=searchGeometry.Width;
              let B_H=searchGeometry.Height;
              let B_L=searchGeometry.Left;
              let B_T=searchGeometry.Top;
              firstPage=docPages[pages[i]-1];
              const {width, height}=firstPage.getSize();
              firstPage.drawRectangle({
                  x: B_L*width,
                  y: (height-(B_T*height))-(B_H*height),
                  width: B_W*width,
                  height: B_H*height,
                  borderColor: rgb(1, 0, 0),
                  borderWidth: 1.5,
              })
              firstPage.drawText(
                  `${pages[i]}`,
                  {
                      x: width-40,
                      y: height-40,
                      size: 20,
                      color: rgb(1, 0, 0),
                      lineHeight: 24,
                      opacity: 0.75,
                  },
              )
          }
              let newPdf=await mergedPdf.copyPages(pdfDoc, pdfDoc.getPageIndices());
              await mergedPdf.addPage(newPdf[pages[i]-1]);
          }
                }
                    await fs.writeFileSync(filePath, await mergedPdf.save());

What actually happened?

It's creating more MB files when compared to the original file even though we are making a pdf with 3 pages.

What did you expect to happen?

The size of the newly created pdf should be less.

How can we reproduce the issue?

By executing above script with any pdf file containing more than 12 pages

Version

"pdf-lib": "^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

Screenshot_2 Screenshot_3

siriv1997 avatar Sep 07 '22 11:09 siriv1997