Hopding / pdf-lib

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

Problems to edit a pdf version 2.0 of acrobat #1497

Open fiorebissi opened 1 year ago

fiorebissi commented 1 year ago

What were you trying to do?

I was trying to add an image to the pdf which has version 2.0 acrobat

How did you attempt to do it?

this is my function how i do it, this function works with pdfs which has 1.7 version

export const processSticker = async ({ numeroFactura, base64 }: { numeroFactura: string, base64: string }) => { try { const docPdf = await PDFDocument.load(base64)

    // HEROKU PATH
    const pngPath = 'https://consejo-profesional.herokuapp.com/STICKER_CONSEJO.png'
    const pages = docPdf.getPages()
    const cantPag = docPdf.getPageCount()

    const pngImageBytes = await fetch(pngPath).then((res) => res.arrayBuffer())

    const Sticker = await docPdf.embedPng(pngImageBytes)

    const pngDims = Sticker.scale(0.25)
    const fontSize = 5

    const helveticaFont = await docPdf.embedFont(StandardFonts.Helvetica)

    for (let i = 0; i < cantPag; i++) {
        if (pages[0].getWidth() > 595 && pages[i].getWidth() <= 612) {
            console.log('-------------------')
            console.log('Pagina Vertical Width', i, pages[i].getWidth())
            console.log('Pagina Vertical Height', i, pages[i].getHeight())
            pages[i].drawImage(Sticker, {
                x: 5,
                y: pages[i].getHeight() - 196.9, // 645
                width: pngDims.width,
                height: pngDims.height
            })

            pages[i].drawText(
                `Nro. Legalización ${numeroFactura}`, {
                    x: 47,
                    y: pages[i].getHeight() - 196.9 + 10, // 655
                    size: fontSize,
                    font: helveticaFont,
                    color: rgb(0.95, 0.95, 0.95),
                    rotate: degrees(90)
                }
            )
        } else {
            if (pages[i].getWidth() === 595 && pages[i].getHeight() === 842) {
                console.log('entre aqui', pages[i].getWidth(), pages[i].getHeight())
                pages[i].drawImage(Sticker, {
                    x: 5, // 398.1
                    y: pages[i].getHeight() - 196.9, // 500
                    width: pngDims.width,
                    height: pngDims.height
                    // rotate: degrees(-90)
                })

                pages[i].drawText(
                    `Nro. Legalización ${numeroFactura}`, {
                        x: 47, // 408.1
                        y: pages[i].getHeight() - 196.9 + 10, // 547
                        size: fontSize,
                        font: helveticaFont,
                        color: rgb(0.95, 0.95, 0.95),
                        rotate: degrees(90)
                    }
                )
            } else {
                console.log('-------------------')
                console.log('Hojas Horizontales Width', i, pages[i].getWidth())
                console.log('Hojas Horizontales Height', i, pages[i].getHeight())

                pages[i].drawImage(Sticker, {
                    x: pages[i].getWidth() - 196.9, // 690
                    y: pages[i].getHeight() - 5, // 400
                    width: pngDims.width,
                    height: pngDims.height,
                    rotate: degrees(-90)
                })
                pages[i].drawText(
                    `Nro. Legalización ${numeroFactura}`, {
                        x: pages[i].getWidth() - 186.9, // 700
                        y: pages[i].getHeight() - 48.3, // 410
                        size: fontSize,
                        font: helveticaFont,
                        color: rgb(0.95, 0.95, 0.95)
                    }
                )
            }
        }
    }

    const pdfDataUri = await docPdf.saveAsBase64({ dataUri: true })

    return pdfDataUri
} catch (error) {
    return error
}

}

What actually happened?

The library pdf-lib is taking the pdf measurements wrong, my pdf has measurements 595pt of width and 842pt of Height, when i make a console.log of width and height it shows that the width is 595 which is OK, but the height shows it in negative, it shows -842pts, So this makes it not find the position Y in the pdf correctly and it does not place the image correctly where I indicate it

What did you expect to happen?

Give me back the pdf with the image added

How can we reproduce the issue?

download the proyect in my github https://github.com/fiorebissi/pdf-lib-prueba.git install dependencies of the proyect using npm or yarn pull up the proyect using yarn dev

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

with pdf files that have 1.7 version it works OK, with higher versions it does not work, it takes bad measurments of the PDF

joshkay commented 6 months ago

@fiorebissi I'm currently running into the same issue. Did you figure out anyway to get around this issue?