java生成pdf并上传oss服务

 

需要的依赖包
<
dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.10</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <!--aliyun --> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>2.8.2</version> </dependency>
// 1-创建文本对象 Document  
        Document document = new Document(PageSize.A4, 500, 150, 50, 50);  
  
        // 2-初始化 pdf输出对象 PdfWriter  
        PdfWriter.getInstance(document, out);  
  
        // 3-打开 Document  
        document.open();  
  
        // 4-往 Document 添加内容  
        document.add(new Paragraph("Hello! PDF!!!"));  
  
        // 5-关闭 Document  
        document.close(); 

直接上代码示例

 private String uploadProtoPdf(String title, String orgName, String templateFront, String templateAfter, Authorization authorization, Map<String, String> itemPro) {
        final String FILE_PATH_TEMPLATE = System.getProperty("java.io.tmpdir") + "/tempdf/%s";
        File outFile = null;
        try {
            outFile = new File(String.format(FILE_PATH_TEMPLATE, UUID.randomUUID() + ".pdf"));
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            Rectangle rectangle = new Rectangle(PageSize.A4);
            Document document = new Document(rectangle, 50, 50, 50, 50);
            PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(outFile));
            document.open();
            // 设置pdf的基础样式;参数1:(1)使用iText中的字体,字体类行为宋体,(2)使用资源字体classpath:/SIMYOU.TTF (3)使用windows系统字体C:/Windows/Fonts/SIMYOU.TTF
            BaseFont baseFont = BaseFont.createFont("C:/WINDOWS/Fonts/simfang.ttf",  BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            // 文档标题对应的样式 参数1:相当于字体(宋体) 参数2:字体大小 参数3:是否加粗,斜体 参数4:字体颜色
            Font headerFont = new Font(baseFont, 20, Font.NORMAL, BaseColor.BLACK);
            // headerParagraph  文档标题
            Paragraph headerParagraph = new Paragraph(title, headerFont);
            ///文字居中
            headerParagraph.setAlignment(Paragraph.ALIGN_CENTER);
            //将添加到文档中
            document.add(headerParagraph);
            //设置段落字体 参数1:设置基础样式 参数2:设置字体大小  参数2:字体正常字体 参数4:设置字体颜色为黑色
            Font paragraphHeaderFont = new Font(baseFont, 18, Font.NORMAL, BaseColor.BLACK);
            // 使用公共样式不单独设置段落样式

            Font fontStyle = new Font(baseFont, 18, Font.UNDERLINE, BaseColor.BLACK);
            Phrase orgPhrase = new Phrase();
            orgPhrase.add(new Chunk(orgName + ":", fontStyle));
            orgPhrase.setLeading(50);
            document.add(orgPhrase);
            templateFront = templateFront.replace("customerName", authorization.getComyName());
            String[] front = templateFront.split("<br>");
            for (String str : front
            ) {
                Paragraph paragraph = new Paragraph(str, paragraphHeaderFont);
                paragraph.setFirstLineIndent(35);
                paragraph.setLeading(40);
                document.add(paragraph);
            }
            int index = 0;
            for (Map.Entry<String, String> entry : itemPro.entrySet()) {
                Paragraph paragraph = new Paragraph(++index + "." + entry.getValue(), paragraphHeaderFont);
                paragraph.setFirstLineIndent(35);
                paragraph.setLeading(40);
                document.add(paragraph);
            }
            templateAfter = templateAfter.replace("date", DateUtil.today());
            String after[] = templateAfter.split("<br>");
            for (String str : after
            ) {
                Paragraph afterParagraph = new Paragraph(str, paragraphHeaderFont);
                afterParagraph.setFirstLineIndent(35);
                afterParagraph.setLeading(40);
                document.add(afterParagraph);
            }
            document.close();
            MultipartFile multipartFile = FileUtils.FileToIo(outFile, "pdf");
            MultiValueMap<String, Object> request = new LinkedMultiValueMap<>();
            request.add("file", multipartFile.getResource());
            request.add("group", fileConfig.getGroup());
            String fileId = restTemplate.postForObject(fileConfig.getUrl(), request, String.class);
            return fileId;
        } catch (IOException e) {
        } catch (DocumentException e) {
        } finally {
            outFile.delete();
        }
        return null;

    }

 文章参考 https://www.cnblogs.com/ssslinppp/p/4976922.html






原文地址:https://www.cnblogs.com/likun10579/p/12894788.html