The need for a PDF to picture has been used in recent projects and is documented here.
1. Based on Ghostscript
Use this method to require the running environment to install Ghostscript. The command used by the conversion is: Gs-sdevice=pngalpha-o%03d.png-sdevice=pngalpha-r144 test.pdf
Public Staticlist<byte[]> pdf2image (String pdffilepath)throwsexception{File tempdir=NULL; Try{tempdir=Files.createtempdir (); Process proc=NewProcessbuilder ("GS", "-sdevice=pngalpha", "-o", TempDir + file.separator + "%03d.png", "-sdevice=pngalpha", "-r144", Pdffilepath). Redirecterrorstream (true). Start (); ArrayList<String> output =NewArraylist<string>(); BufferedReader BR=NewBufferedReader (NewInputStreamReader (Proc.getinputstream ())); String Line=NULL; while(line = Br.readline ())! =NULL) Output.add (line); Logger.info ("Output of the Execute GS command:" +stringutils.join (output, System.lineseparator ())); if(0! =proc.waitfor ())Throw NewException ("Conversion failed"); file[] Files=Tempdir.listfiles (); Arrays.sort (Files,NewComparator<file>() { Public intCompare (file f1, file F2) {returnf1.getname (). CompareTo (F2.getname ()); } }); List<byte[]> images =NewArraylist<>(); for(File file:files) Images.add (Ioutils.tobytearray (Newfileinputstream (file)); returnimages; }finally{ if(TempDir! =NULL) fileutils.deletedirectory (tempdir); } }
Ghostscript also has a lot of common commands, interested can go to see: https://www.ghostscript.com/doc/current/Use.htm
2. Based on ImageMagick
But my project is to have a multi-page PDF into a picture, Ghostscript always turn it into multiple images (I have been looking for a long time online, did not find a picture of the order, if there are small partners know, but also want to share), so I found on the internet ImageMagick , the main thing was to find the command to convert the entire PDF to a picture,
The specific execution command is: Convert Test.pdf-append-flatten test.png
Of course you need to install ImageMagick,
The installation command is: Yum install ImageMagick Imagemagick-devel
Java PDF to pictures