如何在 Nodejs 中读取受密码保护的 PDF 文件并将其放入缓冲区?

How to read password protected PDF file in Nodejs and get it in buffer?

我尝试使用 pdfjs-dist。 越来越大 json 响应。

var PDFJS=require('pdfjs-dist');
PDFJS.getDocument({ url: 'p1.pdf', password: '' }).then(function(pdf_doc) 
{
console.log(pdf_doc);
}).catch(function(error) {
// incorrect password

// error is an object having 3 properties : name, message & code
});

回应 这是我得到的全部回应。 但我需要在缓冲区中做出回应。 可以转buffer吗

    PDFDocumentProxy {
    loadingTask:
    { _capability:
     { resolve: [Function], reject: [Function], promise: [Promise] },
 _transport:
  WorkerTransport {
    messageHandler: [Object],
    loadingTask: [Circular],
    commonObjs: [Object],
    fontLoader: [GenericFontLoader],
    _params: [Object],
    CMapReaderFactory: [DOMCMapReaderFactory],
    destroyed: false,
    destroyCapability: null,
    _passwordCapability: null,
    _networkStream: [PDFNodeStream],
    _fullReader: [PDFNodeStreamFsFullReader],
    _lastProgress: [Object],
    pageCache: [],
    pagePromises: [],
    downloadInfoCapability: [Object],
    numPages: 4,
    pdfDocument: [Circular] },
 _worker:
  { name: null,
    destroyed: false,
    postMessageTransfers: true,
    verbosity: 1,
    _readyCapability: [Object],
    _port: [LoopbackPort],
    _webWorker: null,
    _messageHandler: [Object] },
 docId: 'd0',
 destroyed: false,
 onPassword: null,
 onProgress: null,
 onUnsupportedFeature: null },
 _pdfInfo:
 { numPages: 4,
 fingerprint: '3432353738363537336c6e665361446f6f744f4a70' },
_transport:
  WorkerTransport {
 messageHandler:
  { sourceName: 'd0',
    targetName: 'd0_worker',
    comObj: [LoopbackPort],
    callbackId: 1,
    streamId: 1,
    postMessageTransfers: true,
    streamSinks: [Object],
    streamControllers: [Object: null prototype] {},
    callbacksCapabilities: [Object: null prototype] {},
    actionHandler: [Object],
    _onComObjOnMessage: [Function] },
 loadingTask:
  { _capability: [Object],
    _transport: [Circular],
    _worker: [Object],
    docId: 'd0',
    destroyed: false,
    onPassword: null,
    onProgress: null,
    onUnsupportedFeature: null },
 commonObjs: { objs: [Object: null prototype] {} },
 fontLoader:
  GenericFontLoader {
    docId: 'd0',
    nativeFontFaces: [],
    styleElement: null,
    loadingContext: [Object],
    loadTestFontId: 0 },
 _params:
  [Object: null prototype] {
    url: 'p1.pdf',
    password: '',
    rangeChunkSize: 65536,
    CMapReaderFactory: [Function: DOMCMapReaderFactory],
    ignoreErrors: true,
    pdfBug: false,
    nativeImageDecoderSupport: 'none',
    maxImageSize: -1,
    isEvalSupported: true,
    disableFontFace: true,
    disableRange: false,
    disableStream: false,
    disableAutoFetch: false,
    disableCreateObjectURL: false },
 CMapReaderFactory: DOMCMapReaderFactory { baseUrl: null, isCompressed: false },
 destroyed: false,
 destroyCapability: null,
 _passwordCapability: null,
 _networkStream:
  PDFNodeStream {
    source: [Object],
    url: [Url],
    isHttp: false,
    isFsUrl: true,
    httpHeaders: {},
    _fullRequest: [PDFNodeStreamFsFullReader],
    _rangeRequestReaders: [Array] },
 _fullReader:
  PDFNodeStreamFsFullReader {
    _url: [Url],
    _done: false,
    _storedError: null,
    onProgress: [Function],
    _contentLength: 112979,
    _loaded: 112979,
    _filename: null,
    _disableRange: false,
    _rangeChunkSize: 65536,
    _isStreamingSupported: true,
    _isRangeSupported: true,
    _readableStream: [ReadStream],
    _readCapability: [Object],
    _headersCapability: [Object] },
 _lastProgress: { loaded: 112979, total: 112979 },
 pageCache: [],
 pagePromises: [],
 downloadInfoCapability:
  { resolve: [Function], reject: [Function], promise: [Promise] },
 numPages: 4,
 pdfDocument: [Circular] } }
                                                                                                                                                                                        *ignore below text*

efwrg rgsretg resgerstgh;ergh ;resjgysregh regjes powrjgu oiuueryoeq uieqroeqreqrilih ehr oiyeroeq ioiyeqroeq oieyqrioeq oieqyr oiyeqr oiyeqrp ioqyet oiehr oiyerh oieyreq oiyheqri iohereqk ioheqr qerioyqereq ioehqriheq rioqehriqeb ioeqrhpeq ioeqrhiqe ioqehriq ioqerhioq oirhqeipor oiqehrieq ioehqrq ioeqhrieq iohqerpq ieqhrpeq ioeqhrpeq iheqrpqe oiehrpqe ieqhrqierh ioeqhr ieqhr ioeqrh piqerh ieqhr iheqr piheqr ioheqr iheqr ioeqhrp ioqhre oieqhr oeqiyr qoeiryf pouqer poqure pouqr pouqre[q poquerq poqeur[q poqeur poqwuer poquer[poqwur[wq poqr[poqwhr powrq pow

您可以像下面这样打开并阅读受密码保护的 PDF。使用现有代码:

var PDFJS = require('pdfjs-dist');
PDFJS.getDocument({ url: 'p1.pdf', password: '' }).then(function(pdf) 
{
  let text = [];
  for(let i = 1; i <= pdf.numPages; i++) {
    pdf.getPage(i).then(function(page) {
      page.getTextContent().then(function(data) {
        for(let j = 0; j < data.items.length; j++) {
          text.push(data.items[j].str);
        }        
      });
    });
  }

}).catch(function(error) {
// incorrect password

// error is an object having 3 properties : name, message & code
});