简体   繁体   中英

ImageMagick: PDF to PNG nodejs without using files?

I need to create an endpoint to render a pdf to an image in nodejs and after trying different approaches imagemagick/ghostscript seem to be the best option.

However they expect files as input and output.

Is there a way to only work in memory/variables instead of messing with filesystem (performance/resources is a concern here)?

The ideal scenario is to provide a pdf as base64 string to imagemagick and get an image in base64 string as well.

But since imagemagick is a command line tool I have no idea how to accomplish that and if it's possible at all? Maybe there is a way to hook into the streaming process so I could override where the data goes (like somehow overriding stdin/stdout of imagemagick process or whatever to accumulate the data in a variable instead of sending it to a file)

Or if it's a dead-end, what are alternatives to render pdf to png in node without using files? Tried pdf.js which sort of does it but it appeared to be very unreliable in node for this.

Any advice is much appreciated

sharp can do this. It uses poppler for PDF rendering, rather than shelling out to Ghostscript, so you can do everything in process and without touching the filesystem. It'll render PDFs progressively too, so memory use stays low.

The difficulty you'll have is that poppler is GPL, so the binaries that sharp ships don't include it. You'll need to install a system version of libvips (the underlying image processing library that sharp uses) that includes PDF support. You'll also need to be aware that having a GPL library linked into your node program makes your code GPL too. This means if you plan to sell your program, you'll have to give away the source. Selling access to your program is fine, of course.

Imagemagick is a great tool, but it will process PDFs via ghostscript using a set of large temporary files.

In ImageMagick command line, you can convert base64 files using the INLINE: mechanism. For example:

convert 'inline:data:image/gif;base64,
      R0lGODlhIAAgAPIEAAAAAB6Q/76+vvXes////wAAAAAAAAAAACH5BAEAAAUALAAA
      AAAgACAAAAOBWLrc/jDKCYG1NBcwegeaxHkeGD4j+Z1OWl4Yu6mAYAu1ebpwL/OE
      YCDA0YWAQuJqRwsSeEyaRTUwTlxUqjUymmZpmeI3u62Mv+XWmUzBrpeit7YtB1/r
      pTAefv942UcXVX9+MjNVfheGCl18i4ddjwwpPjEslFKDUWeRGj2fnw0JADs=
    '  b64_noseguy.gif

or to pipe the output to some other tool:

convert 'inline:data:image/gif;base64,
      R0lGODlhIAAgAPIEAAAAAB6Q/76+vvXes////wAAAAAAAAAAACH5BAEAAAUALAAA
      AAAgACAAAAOBWLrc/jDKCYG1NBcwegeaxHkeGD4j+Z1OWl4Yu6mAYAu1ebpwL/OE
      YCDA0YWAQuJqRwsSeEyaRTUwTlxUqjUymmZpmeI3u62Mv+XWmUzBrpeit7YtB1/r
      pTAefv942UcXVX9+MjNVfheGCl18i4ddjwwpPjEslFKDUWeRGj2fnw0JADs=
    '  GIF:- | some_other_tool - ...


Produces:

在此处输入图片说明

See https://imagemagick.org/Usage/files/#inline

ADDITION:

If you want to read one base64 format and write to a variable holding a different base64 form, you can do that with INLINE: in ImageMagick as follows:

Read base64 gif and convert to base64 jpg and save to a variable:

base64jpg=$(convert 'inline:data:image/gif;base64,
R0lGODlhIAAgAPIEAAAAAB6Q/76+vvXes////wAAAAAAAAAAACH5BAEAAAUALAAA
AAAgACAAAAOBWLrc/jDKCYG1NBcwegeaxHkeGD4j+Z1OWl4Yu6mAYAu1ebpwL/OE
YCDA0YWAQuJqRwsSeEyaRTUwTlxUqjUymmZpmeI3u62Mv+XWmUzBrpeit7YtB1/r
pTAefv942UcXVX9+MjNVfheGCl18i4ddjwwpPjEslFKDUWeRGj2fnw0JADs=
' INLINE:JPG:-)


Now read the base64 jpg and convert to PNG:

convert inline:$base64jpg noseguy_b64.png


在此处输入图片说明

But JPG does not support transparency, so it is lost when saving to JPG. Thus the background now is black in place of the original transparent.

ADDITION 2:

Convert the GIF to base64 PDF and then convert that to PNG works fine for me.

base64pdf=$(convert 'inline:data:image/gif;base64,
R0lGODlhIAAgAPIEAAAAAB6Q/76+vvXes////wAAAAAAAAAAACH5BAEAAAUALAAA
AAAgACAAAAOBWLrc/jDKCYG1NBcwegeaxHkeGD4j+Z1OWl4Yu6mAYAu1ebpwL/OE
YCDA0YWAQuJqRwsSeEyaRTUwTlxUqjUymmZpmeI3u62Mv+XWmUzBrpeit7YtB1/r
pTAefv942UcXVX9+MjNVfheGCl18i4ddjwwpPjEslFKDUWeRGj2fnw0JADs=
' INLINE:PDF:-)

convert inline:$base64pdf noseguy_b64.png


在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM