(pdfFile)
| 3729 | * @returns {Promise<Uint8Array>} flatPdf - PDF file in Uint8Array |
| 3730 | */ |
| 3731 | export const flattenPdf = async (pdfFile) => { |
| 3732 | const pdfDoc = await PDFDocument.load(pdfFile, { ignoreEncryption: true }); |
| 3733 | |
| 3734 | let form; |
| 3735 | try { |
| 3736 | form = pdfDoc.getForm(); |
| 3737 | } catch { |
| 3738 | // No form, nothing to flatten |
| 3739 | return await pdfDoc.save({ useObjectStreams: false }); |
| 3740 | } |
| 3741 | |
| 3742 | const pages = pdfDoc.getPages(); |
| 3743 | |
| 3744 | const helvetica = await pdfDoc.embedFont(StandardFonts.Helvetica); |
| 3745 | const helveticaBold = await pdfDoc.embedFont(StandardFonts.HelveticaBold); |
| 3746 | const zapf = await pdfDoc.embedFont(StandardFonts.ZapfDingbats); |
| 3747 | |
| 3748 | const fields = form.getFields(); |
| 3749 | |
| 3750 | for (const field of fields) { |
| 3751 | const type = field.constructor.name; |
| 3752 | |
| 3753 | if (type === "PDFSignature") { |
| 3754 | continue; |
| 3755 | } |
| 3756 | |
| 3757 | const widgets = _safeGetWidgets(field); |
| 3758 | |
| 3759 | for (const widget of widgets) { |
| 3760 | const rect = _getWidgetRect(widget, pdfDoc); |
| 3761 | const page = _getWidgetPage(pdfDoc, pages, widget); |
| 3762 | |
| 3763 | if (!rect || !page) continue; |
| 3764 | |
| 3765 | _drawWidgetBox(page, rect); |
| 3766 | |
| 3767 | if (type === "PDFTextField") { |
| 3768 | _drawTextField(page, field, rect, helvetica); |
| 3769 | } else if (type === "PDFCheckBox") { |
| 3770 | _drawCheckBox(page, field, rect, zapf); |
| 3771 | } else if (type === "PDFRadioGroup") { |
| 3772 | _drawRadioGroup(page, field, widget, rect); |
| 3773 | } else if (type === "PDFDropdown") { |
| 3774 | _drawDropdown(page, field, rect, helvetica); |
| 3775 | } else if (type === "PDFOptionList") { |
| 3776 | _drawOptionList(page, field, rect, helvetica); |
| 3777 | } else if (type === "PDFButton") { |
| 3778 | // Push buttons are interactive controls, not meaningful data fields. |
| 3779 | } |
| 3780 | } |
| 3781 | } |
| 3782 | |
| 3783 | if (fields.length > 0) { |
| 3784 | _removeWidgetAnnotations(pdfDoc); |
| 3785 | } |
| 3786 | |
| 3787 | return await pdfDoc.save({ useObjectStreams: false }); |
| 3788 | }; |
no test coverage detected