Pdfdraw.export does not exist

Product: PDFTron Web

Product Version: Latest

Unable to convert pdf files to multi-page G4 .tiff files using JavaScript (examples do not work)

I’d like to convert PDF files to G4 .tiff files using Javascript. I’ve been looking at a few examples but nothing seems to work. When I create the pdfdraw object it tells me that there are no “export” functions so I can’t export. Do you have a working example of taking a .pdf file (from a URL) and converting it to a G4 tiff file using Javascript? I’ve tried looking at this example (https://www.pdftron.com/documentation/samples/js/PDFDrawTest/) but “exportStream” does not exist on pdfdraw. It also looks like it is creating multiple files (one for each page), I’d prefer to convert the PDF to 1 single multipage .tiff that is using G4 compression. Any help is appreciated.

I’d also be willing to try just converting it back to a grayscale PDF if that’s easier.

Thanks!

Sample code taken directly from the example above (have not tried converting to G4 because the exportStream function does not even exist.

const doc = await PDFNet.PDFDoc.createFromURL(this.documentUrl);
doc.initSecurityHandler()
doc.lock();

console.log(‘PDFNet and PDF document initialized and locked’);

const pdfdraw: PDFNet.PDFDraw = await PDFNet.PDFDraw.create(92);
const itr = await doc.getPageIterator(1);
const currPage = await itr.current();
const pngBuffer = await pdfdraw.exportStream(currPage, ‘PNG’);

exportStream does not exist.

That web page does not contain all the code for the sample, its just a snippet. Please do the following.

  1. Download WebViewer
  2. Unzip in a folder hosted by your server
  3. Run the samples/full-apis/PDFDrawTest sample

From the folder above you can see all the code used.

Thanks Ryan,

I’ll download that sample today and see if it helps.

Thanks again!

I looked at the PDFDraw sample and it wasn’t really helpful. We’re using an angular project so all of our code is in TS (as pasted above) so I’m still not sure how to get the export functions to be available.

Also, that example is still just splitting a multipage PDF up into multiple files and saving each one to disk. I need it to convert a multipage PDF to a multipage G4 tiff to a stream that I can upload to S3 - not have 1 file for each page and not saving to disk. Any suggestions?

We’re using an angular project so all of our code is in TS (as pasted above) so I’m still not sure how to get the export functions to be available.

This guide is the simplest one showing how to integrate our PDFNetJS SDK (with PDFDraw class) into any web site.

I need it to convert a multipage PDF to a multipage G4 tiff to a stream that I can upload to S3 - not have 1 file for each page and not saving to disk.

The PDFDraw.exportStream is a new API, so perhaps you are on an older version, in which case PDFDraw.exportBuffer is an option.
https://www.pdftron.com/api/web/Core.PDFNet.PDFDraw.html#exportBuffer__anchor

As for merging the TIFF images you would have to use a 3rd party library to merge them together. I assume there is a decent javascript TIFF library that will do this for you.

And yes, no disk access is required, the TIFF data will be in memory for you.

all of our code is in TS (as pasted above) so I’m still not sure how to get the export functions to be available.

We also have TypeScript APIs for the PDFDraw class. I think at this time there is no specific guide for accessing PDFDraw TypeScript API but the guide below will hopefully get you started.

Thanks Ryan,

I followed the link you gave me, cloned the repo, followed the instructions (ran NPM install) and then tried to run the sample and get the following error:

C:\Git\webviewer-typescript-sample\node_modules\parcel\src\builtins\css-loader.js: Invalid Version: undefined
at new SemVer (C:\Git\webviewer-typescript-sample\node_modules@babel\preset-env\node_modules\semver\semver.js:314:11)
at compare (C:\Git\webviewer-typescript-sample\node_modules@babel\preset-env\node_modules\semver\semver.js:647:10)
at lt (C:\Git\webviewer-typescript-sample\node_modules@babel\preset-env\node_modules\semver\semver.js:688:10)
at C:\Git\webviewer-typescript-sample\node_modules@babel\preset-env\lib\index.js:275:22
at Object.default (C:\Git\webviewer-typescript-sample\node_modules@babel\helper-plugin-utils\lib\index.js:22:12)
at getEnvPlugins (C:\Git\webviewer-typescript-sample\node_modules\parcel\src\transforms\babel\env.js:62:34)
at getEnvConfig (C:\Git\webviewer-typescript-sample\node_modules\parcel\src\transforms\babel\env.js:12:25)
at async getBabelConfig (C:\Git\webviewer-typescript-sample\node_modules\parcel\src\transforms\babel\config.js:32:19)
at async babelTransform (C:\Git\webviewer-typescript-sample\node_modules\parcel\src\transforms\babel\transform.js:6:16)
at async JSAsset.pretransform (C:\Git\webviewer-typescript-sample\node_modules\parcel\src\assets\JSAsset.js:83:5)

Any idea why I’d be getting that from a fresh repo pull?

Thanks!

Hello jfkruger,

I was able to narrow down this issue to a dependency in that sample. It would seem the latest version of the “parcel” npm package has some issues. I will update the repo, but for a faster fix, you can update the package dependency yourself.

image
Change the red line above in the package.json to the green line and run npm i

This should fix the error. Let me know if there are any issues.

Best Regards,
Ahmad Moaaz
Software Developer
PDFTron Systems, Inc.
www.pdftron.com

Sorry it’s taken me a while to get back to you. That works for the sample app. Thanks.