Hopding / pdf-lib

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

[Help] Issue with embedding images converting pdf. #1577

Open YutoOikawa opened 7 months ago

YutoOikawa commented 7 months ago

What were you trying to do?

I am trying to create a pdf file from multiple images.

How did you attempt to do it?

        async downloadImageFiles(documentFiles){
            const groupDocumentFiles = this.groupByCreatedAtAndCreatedBy(documentFiles)
            let downloadImageCount = 1
            for(let i = (groupDocumentFiles.length - 1);i >= 0;i--) {
                const pdfDoc = await PDFDocument.create()
                for(let j = 0; j < groupDocumentFiles[i].files.length; j++) {
                    await fileService.getBinaryFile(groupDocumentFiles[i].files[j].id)
                    .then(async response => {
                        const fileArrayBuffer = new Uint8Array(response.data)
                        const type = groupDocumentFiles[i].files[j].fileContentType
                        let image
                        if(type === "jpg" || type === "jpeg"){
                            image = await pdfDoc.embedJpg(fileArrayBuffer)
                        }else if(type === "png"){
                            image = await pdfDoc.embedPng(fileArrayBuffer)
                        }

                        const page = pdfDoc.addPage()
                        const imageWidth = image.width
                        const imageHeight = image.height
                        page.setSize(imageWidth,imageHeight)
                        page.drawImage(image)
                    }).catch(error => {
                        console.log(error)
                    })
                }
                const pdfBytes = await pdfDoc.save()
                const blob = new Blob([pdfBytes], { type: "application/pdf" });
                const url = URL.createObjectURL(blob)
                const a = document.createElement("a")
                a.href = url
                a.download = this.setFileName(groupDocumentFiles[i].createdAt,downloadImageCount,consts.FILE_CONTENT_TYPE_PDF)
                a.click()
                downloadImageCount++
            }
        },

Sometimes the downloaded pdf is blank.

What actually happened?

I was still a beginner and didn't understand anything. Please help me.

What did you expect to happen?

I thought it would download properly.

How can we reproduce the issue?

sry I can't reproduce it yet.

Version

1.17.1

What environment are you running pdf-lib in?

Node

Checklist

Additional Notes

No response

Ferhatduran55 commented 7 months ago

I have the same problem with my code, it gets blank page every time Did you find any solution to this problem?

amaliaywalter commented 6 months ago

Hello! maybe when you call page.drawImage(image) you should add parameters like position x and y, width , height and rotation angle.

YutoOikawa commented 6 months ago

I have the same problem with my code, it gets blank page every time Did you find any solution to this problem?

In my case, the image file uploaded had wrong format by checking its binary data, so it got blank after converting. The code seems to be fine till now.

YutoOikawa commented 6 months ago

Hello! maybe when you call page.drawImage(image) you should add parameters like position x and y, width , height and rotation angle.

Thank you for the reply. I got the problem solved!