Convert PDF to image without webviewer UI

WebViewer Version: 8.2

Hello, I’m trying to reproduce the following guide Convert PDF to image in JavaScript Without Webviewer UI but having trouble with the PDFDoc.

When executing the following line const doc = await PDFNet.PDFDoc.createFromURL(url);, it throws the follwing error Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'createFromURL') at <anonymous>:17:41.

I think it’s because the FullAPI is not enabled correctly, so I went to the API documentation for the enableFullPDF() and encountered that this method is deprecated since version 8.0, could you give me an updated example on how to accomplish the same as that guide but with 8.2?

Thanks!

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi,

I think you can just call Core.enableFullPDF(); in v8.2. What the documentation means is that the fullAPI parameter in WebViewer constructor is deprecated.

Thanks.

Wanbo

Then I don’t know why it’s not working, I have the following code:

<html>
  <body>
      <h1>HI!</h1>
    <script src="./lib-8.2.0/core/webviewer-core.min.js"></script>
    <script>
    (async function() {
      Core.setWorkerPath('./lib-8.2.0/core');
      Core.enableFullPDF();
      const licenseKey = 'license here';
      await PDFNet.initialize(licenseKey);

      console.log(PDFNet.PDFDoc);
      const doc = await PDFNet.PDFDoc.createFromURL('http://localhost/path/to/preview_file.pdf');
      const pdfdraw = await PDFNet.PDFDraw.create(92);
      const itr = await doc.getPageIterator(1);
      const currPage = await itr.current();  
      const pngBuffer = await pdfdraw.exportStream(currPage, 'PNG');
      console.log(pngBuffer);
    //   const tifBuffer = await pdfdraw.exportStream(currPage, 'TIFF');
    })()
    </script>
  </body>
</html>

As you can see it’s basically copied from the guide I told you about, if I run the code the console prints the following:

undefined
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'createFromURL')
    at generateThumbnail.html:13:39

I don’t know what I am doing wrong since I’ve tested all the paths and they are correct.
I’ve also tried different PDF URLs and none have worked.

Thanks,
Sergi

Hi,

We’re looking into this, thanks for your patience.

Wanbo

Maybe try using const PDFNet = Core.PDFNet; ?

Thanks.

Wanbo

The new code I’m using is:

<html>
  <body>
      <h1>HI!</h1>
    <script src="./lib-8.2.0/core/webviewer-core.min.js"></script>
    <script>
    (async function() {
      const PDFNet = Core.PDFNet;
      console.log(PDFNet);
      Core.enableFullPDF();
      Core.setWorkerPath('./lib-8.2.0/core');
      const licenseKey = 'license key here';
      await PDFNet.initialize(licenseKey);
      
      console.log(Core);
      console.log(PDFNet);
      console.log(PDFNet.PDFDoc);
      const doc = await PDFNet.PDFDoc.createFromURL('http://localhost/path/to/preview_file.pdf');
      const pdfdraw = await PDFNet.PDFDraw.create(92);
      const itr = await doc.getPageIterator(1);
      const currPage = await itr.current();  
      const pngBuffer = await pdfdraw.exportStream(currPage, 'PNG');
      console.log(pngBuffer);
    //   const tifBuffer = await pdfdraw.exportStream(currPage, 'TIFF');
    })()
    </script>
  </body>
</html>

and the new output in the console is:

generateThumbnail.html?id=1446:8:
Proxy {hasFullAPI: false, hasFullApi: false, Convert: {…}, length: 0, initialize: ƒ, …}

WARNING PDFworker.js?isfull=true&disableLogs=0&wasm=1:48:
There may be some degradation of performance. Your server has not been configured to serve .gz. and .br. files with the expected Content-Encoding. See http://www.pdftron.com/kb_content_encoding for instructions on how to resolve this.

WARNING PDFworker.js?isfull=true&disableLogs=0&wasm=1:48:
There may be some degradation of performance. Your server has not been configured to serve .gz. and .br. files with the expected Content-Encoding. See http://www.pdftron.com/kb_content_encoding for instructions on how to resolve this.

generateThumbnail.html?id=1446:14:
ƒ (){}

generateThumbnail.html?id=1446:15:
Proxy {hasFullAPI: false, hasFullApi: false, Convert: {…}, messageHandler: aa, initialize: ƒ, …}[[Handler]]: Object[[Target]]: ƒ ()[[IsRevoked]]: false

generateThumbnail.html?id=1446:16:
undefined

ERROR generateThumbnail.html?id=1446:17:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'createFromURL') 
at generateThumbnail.html?id=1446:17:39

The final error is basically the same, I have also included the output of the log for the Core and PDFNet and it seems like the hasFullAPI is still false after initializing. I’ve tried moving the const PDFNet = Core.PDFNet; around but the output is always the same.

Hi,

Please take a look at this guide. Looks like you also need include the PDFNet.js in your html.

Thanks.

Wanbo

Yep, that was it, thank you!