依赖
< dependency> < groupId> org.apache.pdfbox</ groupId> < artifactId> pdfbox</ artifactId> < version> 2.0.13</ version> </ dependency>
工具类
import org. apache. pdfbox. pdmodel. PDDocument ;
import org. apache. pdfbox. rendering. ImageType ;
import org. apache. pdfbox. rendering. PDFRenderer ;
import org. slf4j. Logger ;
import org. slf4j. LoggerFactory ;
import sun. misc. BASE64Encoder ; import javax. imageio. ImageIO ;
import java. awt. image. BufferedImage ;
import java. io. ByteArrayOutputStream ;
import java. io. InputStream ;
import java. net. HttpURLConnection ;
import java. net. URL ;
public class PdfToImgUtils { private static Logger log = LoggerFactory . getLogger ( PdfToImgUtils . class ) ; public static final float DEFAULT_DPI = 105 ; public static void main ( String [ ] args) { long start = System . currentTimeMillis ( ) ;
} public static String getBase64 ( String pdfURL) { System . setProperty ( "sun.java2d.cmm" , "sun.java2d.cmm.kcms.KcmsServiceProvider" ) ; int width = 0 ; int [ ] singleImgRGB; int shiftHeight = 0 ; BufferedImage imageResult = null ; InputStream pdfInputStream = null ; ByteArrayOutputStream pdfoutStream = null ; ByteArrayOutputStream bos = null ; PDDocument pdDocument = null ; byte [ ] buffer = null ; try { URL url = new URL ( pdfURL) ; HttpURLConnection conn = ( HttpURLConnection ) url. openConnection ( ) ; conn. setRequestMethod ( "GET" ) ; conn. setConnectTimeout ( 10 * 1000 ) ; conn. setReadTimeout ( 10 * 1000 ) ; pdfInputStream = conn. getInputStream ( ) ; pdfoutStream = new ByteArrayOutputStream ( ) ; buffer = new byte [ 4926 ] ; int lenn = - 1 ; while ( ( lenn = pdfInputStream. read ( buffer) ) != - 1 ) { pdfoutStream. write ( buffer, 0 , lenn) ; } buffer = pdfoutStream. toByteArray ( ) ; pdDocument = PDDocument . load ( buffer) ; PDFRenderer renderer = new PDFRenderer ( pdDocument) ; int pageCount = pdDocument. getNumberOfPages ( ) ; for ( int i = 0 ; i < pageCount; i++ ) { BufferedImage image = renderer. renderImageWithDPI ( i, DEFAULT_DPI , ImageType . RGB ) ; int imageHeight = image. getHeight ( ) ; int imageWidth = image. getWidth ( ) ; if ( i == 0 ) { width = imageWidth; imageResult = new BufferedImage ( width, imageHeight * pageCount, BufferedImage . TYPE_INT_RGB ) ; } else { shiftHeight += imageHeight; } singleImgRGB = image. getRGB ( 0 , 0 , width, imageHeight, null , 0 , width) ; imageResult. setRGB ( 0 , shiftHeight, width, imageHeight, singleImgRGB, 0 , width) ; } bos = new ByteArrayOutputStream ( ) ; ImageIO . write ( imageResult, "jpg" , bos) ; byte [ ] bytes = bos. toByteArray ( ) ; if ( null == bytes) { throw new Exception ( "获取图片为空!" ) ; } String base = new BASE64Encoder ( ) . encode ( bytes) . trim ( ) . replaceAll ( "\n" , "" ) . replaceAll ( "\r" , "" ) ; pdDocument. close ( ) ; return base; } catch ( Exception e) { e. printStackTrace ( ) ; } finally { try { if ( null != pdfInputStream) { pdfInputStream. close ( ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } try { if ( null != pdfoutStream) { pdfoutStream. close ( ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } try { if ( null != bos) { bos. close ( ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } try { if ( null != pdDocument) { pdDocument. close ( ) ; } } catch ( Exception e) { e. printStackTrace ( ) ; } } return null ; } }