java poi pdf 导出

java poi pdf 导出

(java poi pdf导出 文字+图片两张放置一行)
思路:流传入图片 ,pdf没有行的概念,只有列即为一行,两张图片可以先建立一列在一列总再建立两列各放置一张图片 然后调整图片大小即可
入口:

 @PostMapping(value = "/exportDIY")
    @ResponseBody
    @ApiOperation("多格式导出")
    @ControllerLog(description = "多格式导出", logLevel = 6)
    public ResponseResult exportDIY(HttpServletResponse response, @RequestBody Map<String, String> request) {
        return logService.exportDIY(response, request);
    }

主题:
这部分读入流图片写成图片,然后再填充到pdf文件中,临时文件可考虑删除,一直就只有一张。

  FileOutputStream fileOutputStream1 = null;
            FileOutputStream fileOutputStream2 = null;
            ByteArrayInputStream dataChartStringin1 = null;
            ByteArrayInputStream dataChartStringin2 = null;
            try {
                String filename = fileName + ".pdf";
                response.setContentType("application/pdf;chartset=utf-8");
                response.setHeader("Content-Disposition", "attachment;filename=" + filename);
                Rectangle rectPageSize = new Rectangle(PageSize.A4); //  定义A4页面大小
                rectPageSize = rectPageSize.rotate();
                Document document = new Document(rectPageSize, 50, 50, 50, 50);
                PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
                PdfPageEventHelper pdfPageEventHelper = new PdfPageEventHelper();
                pdfPageEventHelper.onEndPage(writer, document);
                document.open();
                addPdfTitleMsg(request, document);
                // 获取流
                BASE64Decoder base64Decoder = new BASE64Decoder();
                String dataChartString1 = URLDecoder.decode(request.get("image1").substring(22), "UTF-8").replaceAll(" ", "+");
                dataChartStringin1 = new ByteArrayInputStream(base64Decoder.decodeBuffer(dataChartString1));    //将picInfoByte作为输入流;
                //读文件
                byte[] b1 = new byte[128];
                String path1 = new ClassPathResource("temp/temp1.png").getFile().getPath();
                File file1 = new File(path1);
                fileOutputStream1 = new FileOutputStream(file1);
                while (dataChartStringin1.read(b1) != -1) {
                    fileOutputStream1.write(b1);
                }
                Image image1 = Image.getInstance(path1);
                image1.scaleAbsolute(280, 120); // 设置图片大小
                image1.setAccessibleAttribute(PdfName.ALT, new PdfString("temp1"));

                String dataChartString2 = URLDecoder.decode(request.get("image2").substring(22), "UTF-8").replaceAll(" ", "+");
                dataChartStringin2 = new ByteArrayInputStream(base64Decoder.decodeBuffer(dataChartString2));    //将picInfoByte作为输入流;
                byte[] b2 = new byte[128];
                String path2 = new ClassPathResource("temp/temp1.png").getFile().getPath();
                File file2 = new File(path2);
                fileOutputStream2 = new FileOutputStream(file2);
                while (dataChartStringin2.read(b2) != -1) {
                    fileOutputStream2.write(b2);
                }
                Image image2 = Image.getInstance(path2);
                image2.scaleAbsolute(280, 120); // 设置图片大小
                image2.setAccessibleAttribute(PdfName.ALT, new PdfString("temp2"));
                // 以上图片 完成
                PdfPTable headerTable = new PdfPTable(2);
                PdfPCell acell = new PdfPCell(image1, false);
                removeBorder(acell);

                headerTable.addCell(acell);
                PdfPCell rCell = new PdfPCell(image2, false);
                removeBorder(rCell);
                headerTable.addCell(rCell);
                //将主要的表格headerTable加入document文档对象中
                document.add(headerTable);
                document.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fileOutputStream1 != null) {
                        fileOutputStream1.close();
                    }
                    if (dataChartStringin1 != null) {
                        dataChartStringin1.close();
                    }
                    if (fileOutputStream2 != null) {
                        fileOutputStream2.close();
                    }
                    if (dataChartStringin2 != null) {
                        dataChartStringin2.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

  private void addPdfTitleMsg(Map<String, String> request, Document document) throws DocumentException {
        PdfUtil.addElement("终端审计日志统计", Element.ALIGN_CENTER, document);

        PdfPTable table = new PdfPTable(1);
        Font font = new Font(FontUtil.getBfChinese(), 12);
        // 换行
        document.add(new Chunk(("\n")));
        // 时间
        PdfUtil.pcellColorNoBorder(table, new Paragraph(getTimeMsg(request), font));
        document.add(table);
    }
/**
     * PDF写入每个列字段,在列内居左,无边框
     *
     * @param table
     * @param p
     */
    public static void pcellColorNoBorder(PdfPTable table, Paragraph p) {
        PdfPCell cell_color = new PdfPCell(p);
        cell_color.setVerticalAlignment(Element.ALIGN_LEFT);// 字居中
        cell_color.setBorder(PdfPCell.NO_BORDER);
        table.addCell(cell_color);
    }

以上postman自测OK
在这里插入图片描述以上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要将Java导出的Excel转换为PDF,您可以使用iText库。iText是一个流行的Java库,可以用于创建和操作PDF文档,其中包括将Excel文件转换为PDF格式。以下是一个简单的示例代码,演示如何使用iText将Excel文件转换为PDF: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfWriter; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; public class ExcelToPdfConverter { public static void main(String[] args) throws Exception { // 读取Excel文件 InputStream input = new FileInputStream(new File("input.xls")); Workbook workbook = new HSSFWorkbook(input); // 创建PDF文档 Document document = new Document(); OutputStream output = new FileOutputStream(new File("output.pdf")); PdfWriter writer = PdfWriter.getInstance(document, output); document.open(); // 将Excel文件内容写入PDF文档 PdfCopy pdf = new PdfCopy(document, output); for (int i = 0; i < workbook.getNumberOfSheets(); i++) { PdfReader reader = new PdfReader(workbook.getBytesAt(i)); pdf.addDocument(reader); } // 关闭文档 document.close(); output.close(); input.close(); } } ``` 在上面的示例中,我们首先使用Apache POI库读取Excel文件,然后使用iText库创建PDF文档。我们将Excel文件的内容写入PDF文档,然后关闭文件流。最后,我们将生成PDF文件保存到磁盘上。请注意,该示例需要iText和Apache POI库的依赖项。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值