PDFJS:在 NodeJS 中从内存缓冲区读取 PDF
注意:本文使用 async/await,因此需要 NodeJS 8+。
以下是如何从文件读取 PDF 文件,例如 mypdf.pdf:
read_pdf.js
pdfjs.getDocument('mypdf.pdf');完整示例:
read_pdf.js
const pdfjs = require('pdfjs-dist');
async function readPDF() {
const pdf = await pdfjs.getDocument('mypdf.pdf');
// ...
}以下是你如何从内存缓冲区读取 PDF:
read_pdf_from_buffer_snippet.js
pdfjs.getDocument({data: buffer});完整示例
read_pdf_from_buffer.js
const fs = require('mz/fs')
const pdfjs = require('pdfjs-dist');
async function readPDF() {
// 将文件读入缓冲区
const buffer = await fs.readFile('mypdf.pdf')
// 从缓冲区解析 PDF
const pdf = await pdfjs.getDocument({data: buffer});
// ...
}使用 mz/fs 不是必需的,它只是用作实用库以便能够对文件使用 await。
Check out similar posts by category:
Javascript, PDF
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow