PDF转图片
效果图
代码
const path = require ( 'path' ) ;
const pdf = require ( 'pdf-poppler' ) ;
const fs = require ( 'fs' ) ;
const pdfFilePath = './path/test.pdf' ;
const opts = { format : 'png' , out_dir : './path/output' , out_prefix : 'outputImg' , page : null
} ;
pdf. convert ( pdfFilePath, opts) . then ( res => { console. log ( 'Successfully converted:' , res) ; } ) . catch ( error => { console. error ( 'Error converting PDF to images:' , error) ; } ) ;
图片转PDF
效果图如下
代码:
const PDFDocument = require ( 'pdfkit' ) ;
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
const doc = new PDFDocument ( ) ;
doc. info. title = 'My Image to PDF' ;
const outputStream = fs. createWriteStream ( path. resolve ( './path/output/output.pdf' ) ) ;
doc. pipe ( outputStream) ;
doc. image ( './path/zgr.jpg' , { width : 500 , height : 300 , fit : [ 500 , 300 ]
} ) ;
doc. end ( ) ; outputStream. on ( 'finish' , ( ) => { console. log ( 'PDF生成完成' ) ;
} ) ; outputStream. on ( 'error' , ( err ) => { console. error ( 'PDF生成出错:' , err) ;
} ) ;