kempsteven / vue-html2pdf

vue-html2pdf converts any vue component or element into PDF, vue-html2pdf is basically a vue wrapper only and uses html2pdf.js behind the scenes.
https://www.npmjs.com/package/vue-html2pdf
MIT License
432 stars 75 forks source link

Get Blob file after pagination #149

Closed rogerio-pereira closed 1 year ago

rogerio-pereira commented 1 year ago

Describe the bug How can i get the pdf BLOB after pagination?

To Reproduce Pdf with pagination is being downloaded normally, i need a way to get the pdf blob, so i can send it to backend, to be processed (store in S3, and send it to email). To paginate i'm using @beforeDownload, but is required to set the props :enable-download and :preview-modal to false

Package Version e.g. ^1.8.0

Additional context Available method hasDownloaded, works, but not when props :enable-download and :preview-modal are false (which is required to paginate the file)

I need the pagination in this case, because it contains the version of the file image

rogerio-pereira commented 1 year ago

Solved. Changed method beforeDownload

async beforeDownload ({ html2pdf, options, pdfContent }) {
    let pdfBlobUrl = await html2pdf()
                                .set(options)
                                .from(pdfContent)
                                .toPdf()
                                .get('pdf')
                                .then((pdf) => {
                                    const totalPages = pdf.internal.getNumberOfPages()
                                    for (let i = 1; i <= totalPages; i++) {
                                        pdf.setPage(i)
                                        pdf.setFontSize(10)
                                        pdf.setTextColor(150)
                                        pdf.text('Page ' + i + ' of ' + totalPages, (pdf.internal.pageSize.getWidth() * 0.88), (pdf.internal.pageSize.getHeight() - 0.3))
                                    } 
                                })
                                .save()
                                .output('bloburl')

    let res = await fetch(pdfBlobUrl);
    let blobFile = await res.blob();
}   

Variable blobFile is the pdf file