Replies: 1 comment 1 reply
-
I am hitting this exact same problem from AWS Lambda. Attempting to read files from an on-premise file share, merge them, and respond with the base64 of the merged PDF. AWS Lambda and AWS API Gateway both have response size limitations. My test PDF before being split up was around 2MB. It's being split up by page by another process. When I attempt to merge each PDF back it's resulting in an 18MB PDF. I cannot seem to find anything out there on what I might be able to do to reduce the filesize. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I use copyPages to merge PDFs, but the output is to large.
And I try embedFont, but it not works, it still to large
the code is blow:
export const mergePDF = async ({pdfFiles,outputFile,cb}) =>{ const fontBytes = fs.readFileSync(join(__dirname, '/font/FZJHSXJW.TTF')) const pdfDoc = await PDFDocument.create() // 处理字体库,为了防止输出体积过大 pdfDoc.registerFontkit(fontkit); await pdfDoc.embedFont(fontBytes,{subset: true}) for(let i=0; i< pdfFiles.length;i++){ const pdfPath = pdfFiles[i] const pdfItem = await PDFDocument.load(fs.readFileSync(pdfPath)) for(let j=0;j<pdfItem.getPageCount();j++){ const [PDFPageItem] = await pdfDoc.copyPages(pdfItem,[j]) pdfDoc.addPage(PDFPageItem) } } const pdfBytes = await pdfDoc.save() // For example,
pdfBytescan be: // • Written to a file in Node // • Downloaded from the browser // • Rendered in an <iframe> fs.writeFileSync(outputFile,pdfBytes) // 合并后清空源文件 clearPdfFiles(pdfFiles) cb && cb() }
Beta Was this translation helpful? Give feedback.
All reactions