小与哥 2016-09-28 08:32 采纳率: 44.4%
浏览 1360
已结题

java pdf 转 jpg 并 作为压缩包 下载

先 通过 httpclient 请求 得到 pdf相关的字节数组,而后,把字节数组转成 pdf文件,再 把 pdf 转成 jpg,再 以压缩包 的形式,输出到页面,
这个问题搞一天了,没搞出来,谁做过,帮我看一下,我贴一下源码:

 HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();
        byte[] body = EntityUtils.toByteArray(entity);

        getFile(body, null);

        pdfToJpg(savePath, zos, i);


    }

    public static void pdfToJpg(String getPdfFilePath, ZipOutputStream zos, int i) throws IOException {
        String savePath = "F:\\" +"test.pdf";
        File file = new File(savePath);
        // load a pdf from a byte buffer
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        PDFFile pdffile = new PDFFile(buf);

        // draw the first page to an image
        PDFPage page = pdffile.getPage(1);

        // get the width and height for the doc at the default zoom
        Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());

        // generate the image
        Image img = page.getImage(rect.width, rect.height, // width &
                                                            // height
                rect, // clip rect
                null, // null for the ImageObserver
                true, // fill background with white
                true // block until drawing is done
        );

        BufferedImage tag = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(img, 0, 0, rect.width, rect.height, null);

        File jpgFile = new File("F:\\" +"test.jpg");
        FileOutputStream out = new FileOutputStream(jpgFile); // 输出到文件流

        ZipEntry entry = new ZipEntry("report"+i+".jpg");
        zos.putNextEntry(entry);

        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        encoder.encode(tag); // JPEG编码
        ImageIO.write(tag, "jpg", zos);

        out.close();

//      raf.close();
//      channel.close();

    }

    /** 
     * 根据byte数组,生成文件 
     */  
    public static void getFile(byte[] bfile, String savePath) { 
        String filePath = "F:\\" +"test.pdf";

        FileOutputStream fos = null; 
        BufferedOutputStream bos = null;
        File file = null;  
        try {  
            file = new File(filePath);
            fos = new FileOutputStream(filePath);  
            bos = new BufferedOutputStream(fos);  
            bos.write(bfile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bos != null) {  
                try {  
                    bos.close(); 
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            if (fos != null) {  
                try {  
                    fos.close(); 
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
        }  
    }
  • 写回答

1条回答

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?