question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resize existing pages?

See original GitHub issue

Can pdf-lib be used to open an existing PDF document and resize the width and/or height of some of its pages?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
creativityjuicecommented, Oct 26, 2021

Here is an update of this code for a more recent version of pdf-lib (v1.17.0). I hope it might help as it would have saved me some times. It’s made to convert a PDF to A4 or US Letter format, but you can easily change it to resize to any format. I use the scale method when the new ratio of the page is similar to the old one. When it’s not the case, I used setSize + scaleContent + translateContent. My code doesn’t deal with annotation and might fail for PDF with different boxes sizes (mediaBox, cropBox, trimBox, …)

const pageSizes = {
    a4: {
        width: 594.96,
        height: 841.92,
    },
    letter: {
        width: 612,
        height: 792,
    },
};
const resizeToPageFormat = async (snapshot: Uint8Array, paper_format: PaperFormat): Promise<Uint8Array> => {
    // Load the original PDF file
    const pdfDoc = await PDFDocument.load(snapshot, {
        parseSpeed: ParseSpeeds.Fastest,
    });

    const new_size = pageSizes[paper_format || 'a4'];
    const new_size_ratio = Math.round((new_size.width / new_size.height) * 100);

    // Get the first page in the PDF
    const pages = pdfDoc.getPages();

    pages.forEach(page => {
        const { width, height } = page.getMediaBox();
        const size_ratio = Math.round((width / height) * 100);
        // If ratio of original and new format are too different we can not simply scale (more that 1%)
        if (Math.abs(new_size_ratio - size_ratio) > 1) {
            // Change page size
            page.setSize(new_size.width, new_size.height);
            const scale_content = Math.min(new_size.width / width, new_size.height / height);
            // Scale content
            page.scaleContent(scale_content, scale_content);
            const scaled_diff = {
                width: Math.round(new_size.width - scale_content * width),
                height: Math.round(new_size.height - scale_content * height),
            };
            // Center content in new page format
            page.translateContent(Math.round(scaled_diff.width / 2), Math.round(scaled_diff.height / 2));
        } else {
            page.scale(new_size.width / width, new_size.height / height);
        }
    });

    // Serialize the modified document
    return pdfDoc.save();
};
1reaction
sendrel-lambdascommented, Jan 29, 2021

Thanks! Managed to modify it to my liking.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scale or resize printed pages in Acrobat and Reader
Manually scale using percentages · Choose File > Print. · From the Page Scaling pop-up menu, select Tile All Pages to expose the...
Read more >
Resizing Pages - PDF Editor PDF Studio User Guide
PDF Studio can resize a page to a specified dimension. This tool can be used to scale pages so that the content better...
Read more >
InDesign Basics: How Do I Change the Size of a Page in My ...
The rule of thumb is: If your page is blank or has very little content, you can just resize using the options available...
Read more >
How to Adjust/ Resize PDF Pages ( Simple & Quick) - YouTube
Resizing PDF Pages In Adobe Acrobat Pro DC.
Read more >
How to Change PDF Page Size in Adobe Acrobat
Then, go to "Page Setup," select the appropriate size from the "Size" option. Finally, click on the "Print," so Adobe Acrobat resize page...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found