File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ const { PDFDocument } = require ( 'pdf-lib' ) ;
2+ const fs = require ( 'fs' ) ;
3+
4+ async function fixBlankPdf ( ) {
5+ try {
6+ console . log ( 'Reading input.pdf...' ) ;
7+ const pdfBytes = fs . readFileSync ( 'input.pdf' ) ;
8+
9+ // Carrega o original
10+ const srcDoc = await PDFDocument . load ( pdfBytes ) ;
11+
12+ // Cria um PDF NOVO do zero
13+ const newDoc = await PDFDocument . create ( ) ;
14+
15+ console . log ( 'Copying and cleaning pages...' ) ;
16+ const pageIndices = srcDoc . getPageIndices ( ) ;
17+
18+ // Esta função copyPages tenta extrair o conteúdo visual de forma independente
19+ const copiedPages = await newDoc . copyPages ( srcDoc , pageIndices ) ;
20+
21+ copiedPages . forEach ( ( page ) => {
22+ newDoc . addPage ( page ) ;
23+ } ) ;
24+
25+ console . log ( 'Saving with compatibility settings...' ) ;
26+ // Tentamos salvar sem compressão para garantir que nada suma
27+ const finalBytes = await newDoc . save ( {
28+ useObjectStreams : false ,
29+ addDefaultFont : true // Tenta forçar uma fonte caso a original tenha sumido
30+ } ) ;
31+
32+ fs . writeFileSync ( 'output_fixed_v2.pdf' , finalBytes ) ;
33+ console . log ( 'Done! Check "output_fixed_v2.pdf"' ) ;
34+ } catch ( err ) {
35+ console . error ( 'Error:' , err ) ;
36+ }
37+ }
38+
39+ fixBlankPdf ( ) ;
You can’t perform that action at this time.
0 commit comments