galkahana / HummusJS

Node.js module for high performance creation, modification and parsing of PDF files and streams
http://www.pdfhummus.com
Other
1.15k stars 170 forks source link

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

Open rezamasoomin opened 3 years ago

rezamasoomin commented 3 years ago

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);
                                    }

                                })

                            });

                        })

                    });

                }

        }

    }