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

unable to append to page, make sure source file exists at merge #458

Open
rezamasoomin opened this issue Apr 27, 2021 · 0 comments
Open

Comments

@rezamasoomin
Copy link

rezamasoomin commented Apr 27, 2021

I get an error when I run this function just only when I use this pdf file.
does have a problem my pdf?
main.pdf
logo.pdf
qrcode.pdf

let temp = 'files/' + uniqueId + '/temp/';
let temp2 = "files/" + uniqueId;

const merg = async (callback) => {

let doc = await pdfjsLib.getDocument('./' + temp2 + '/main.pdf').promise;

        var numPages = doc.numPages;

        for (let i = 0; i < numPages; i++) {

              let pdfWriter = hummus.createWriter('./' + temp + i + '.pdf');

                 var page = pdfWriter.createPage(0, 0, 595, 842);

                try {

                   pdfWriter.mergePDFPagesToPage(page,
                        './' + temp2 + '/main.pdf',
                        {
                             type: hummus.eRangeTypeSpecific,
                            specificRanges: [[i, i]]
                        });

                } catch (e) {

                    console.log(e);

                }

                var contentContext = pdfWriter.startPageContentContext(page).q().cm(0.6, 0, 0, 0.6, 500, 0);

                pdfWriter.mergePDFPagesToPage(page,
                    './' + temp2 + '/qrcode.pdf', {
                        type: hummus.eRangeTypeSpecific,
                        specificRanges: [[0, 0]]
                    });


                contentContext.Q().q().cm(0.5, 0, 0, 0.5, 0, 0);
                pdfWriter.mergePDFPagesToPage(page, './assets/logo.pdf', {
                    type: hummus.eRangeTypeSpecific,
                    specificRanges: [[0, 0]]
                });

                pdfWriter.writePage(page).end();

                if (i == numPages - 1) {
                    fs.readdir('./' + temp, (err, files) => {
                        var outStream = new memoryStreams.WritableStream();
                        fs.readFile('./' + temp + '0.pdf', function (err, StartpdfFile) {
                            var pdfFileToMerg = new hummus.PDFRStreamForBuffer(StartpdfFile);
                            var pdfWriter = hummus.createWriterToModify(pdfFileToMerg, new hummus.PDFStreamForResponse(outStream));
                            i = 0;
                            files.sort(function (a, b) {
                                return a.split(".")[0] - b.split(".")[0]
                            });
                            let combinedfiles = {};
                            files.forEach(file => {

                                fs.readFile('./' + temp + file, function (err, pdfFile) {
                                    try {
                                        combinedfiles[file] = new hummus.PDFRStreamForBuffer(pdfFile);

                                        i++;

                                        if (numPages == i) {

                                            for (let i = 1; i < Object.keys(combinedfiles).length; i++) {
                                                pdfWriter.appendPDFPagesFromPDF(combinedfiles[i + '.pdf'], {
                                                    type: hummus.eRangeTypeSpecific,
                                                    specificRanges: [[0, 0]]
                                                });

                                                if (i == Object.keys(combinedfiles).length - 1) {
                                                    pdfWriter.end();
                                                    var newBuffer = outStream.toBuffer();
                                                    outStream.end();

                                                    fs.writeFile('./' + temp2 + '/last.pdf', newBuffer, 'base64', function (err) {
                                                        return callback(newBuffer);
                                                    })


                                                }

                                            }


                                        }
                                    } catch (e) {
                                        console.log('Error during PDF combination: ' + e.message);
                                    }

                                })


                            });

                        })


                    });


                }




        }


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

No branches or pull requests

1 participant