0

I tried using pdfjs-dist. getting large json response.

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
});

Response This is the whole response I am getting. but I need response in buffer. Can it be converted to 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

7
  • So are you able to read the pdf file now? Commented Jul 2, 2019 at 6:45
  • getting large response in json Commented Jul 2, 2019 at 6:46
  • unable to handle the response Commented Jul 2, 2019 at 6:49
  • What's your end goal? Do you need to get the text from the pdf page by page? Commented Jul 2, 2019 at 6:55
  • Yes I want it in text Commented Jul 2, 2019 at 7:01

1 Answer 1

4

You may open and read a password protected PDF like below. Working with your existing code:

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
});
Sign up to request clarification or add additional context in comments.

6 Comments

getting error (node:11684) UnhandledPromiseRejectionWarning: Error: Invalid page request
pdfreader is able to convert non-password protected pdfs to text. I cannot find any options for password there. do you have any idea ?
I have got the Point. Thnak you so much.
What changes did you make? Maybe I can add it to the answer.
number of pages starts from 1 not 0
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.