Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
YutoOikawa opened this issue Dec 22, 2023 · 4 comments
Open
2 tasks done

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

YutoOikawa opened this issue Dec 22, 2023 · 4 comments

Comments

@YutoOikawa
Copy link

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

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

No response

@Ferhatduran55
Copy link

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

@amaliaywalter
Copy link

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

@YutoOikawa
Copy link
Author

YutoOikawa commented Jan 15, 2024

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants