'How to convert a base64 pdf string to a base64 image string using Javascript?

Please help me to convert a base64 pdf string to a base64 image string using javascript.



Solution 1:[1]

At some stage you will need to use a 3rd party library, thus:-

By far the simplest is to call the systems tools via a single command line or two, here on Windows 7 I am using one line for a single page pdf but would need to use a for loop to handle multiple files or multiple pages or both.

enter image description here

Many Linux and Macs have the base64 utility as standard or on windows it can be one single exe, in the image above we can see the stream of the first few lines of the iVBOR... image text reprex.

For any conversion it is a very very basic

  • Take the input (PDFbase64.Ans or .txt) compress it back into Binary using some AtoB function such as system base64 utility or write that in javascript (there are many examples in Stack Overflow).

  • Convert the Binary.PDF into images using PDFtoImage (there are hundreds of ways but the simplest is PDFtoPNG.exe).

  • Now you have your PDF###.PNG[s] you can use any ImageToBase64 (BtoA) function your javascript programming language uses or again use the system converter.

However beware the result may not be as expected, without some tweaking of each command. So the source image (10,447 bytes) within the PDF (9,364 bytes) was origionally about 100dpi (actually a bit off since there was a stray byte in x and y)! The output was defaulted as 150dpi (14,383 bytes), thus the base64 will be twice expanded compared to the source (19,180 bytes). Hence I would recommend its much simpler and about 4 times as fast to serve the source image rather than text that needs conversion.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1