2

I am in need of a way to get a PDF to a readable string format so I can search for specific values in the PDF. But from what i've found so far Apex is unable to get the blob value converted into a string.

Is there a way in javascript to do this instead? I am thinking of building a lightning component to process all these blobs for me and export the key data into a csv.

Also considering python if javascript cannot do it. I need some way to turn these blobs into string so I can substring key values.

Update 2022: Sorry, I never updated this till now. I decided to do this via Python script rather than Apex/Lightning.

3 Answers 3

2

Apex has a hard time with binary data. It's not impossible to write a PDF parser in Apex, but it would be absurdly time-consuming to implement. PDF.js from Mozilla should be Lightning compatible, and is probably your best bet for a browser-based solution.

2
  • Will pdf.js allow me to get the whole pdf contents and substring the values? A bit lost on where to start. The most I can get out of apex is a base64 string. Feb 25, 2020 at 15:01
  • @AlexanderAtkinsoon I don't know pdf.js well enough to be certain, but it says it can parse PDFs, so it should get the job done. Apex doesn't really have a notion of a "binary" data type that can address individual indices. You can convert base64 to a binary array, but then you still need to parse that using the PDF specs. At least with pdf.js, you should be able to get the contents in a reasonable manner.
    – sfdcfox
    Feb 25, 2020 at 18:59
2

Turning the file into a base64 string using EncodingUtil.base64Encode might help but may soon hit heap and cpu limits.

on the UI level, 3rd party node modules like pdf2json can be used

2
  • Would making a batch class and processing each with batch size 1 be a good option to avoid hitting limits? Feb 24, 2020 at 17:21
  • no as the daily limit on batch per org is 10K 😞sooner or later one or the other governer limit would cause issues...
    – Abhijith E
    Feb 24, 2020 at 22:44
1

It’s many months later, but just seeing this. I had a similar requirements and chose to implement a microservice on Heroku to scrape the text from a PDF. It’s called from Apex which does the final parsing of the returned text. I’ve presented on the topic if you’re interested here: https://youtu.be/O-Ohni7wEKA

The service is tiny, less than 100 lines of code written in GO but could just as easily be JavaScript and calls an open source executable to parse the PDF. Any similar approach would work.

Good luck.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .