java IO创建HTML版PDF

public void viewPDF() {
    String tid = (String) getSessionAttr(SessionConstants.SELECTED_TENANT_ID);
    int personId = getSessionAttr(SessionConstants.USER_PERSON_ID_INT);
    Person applyPerson = PersonService.service.getPersonById(personId);
    String savePath = PropKit.get("uploadedFileSaveDirectory") + tid + File.separator + "sys" + File.separator;
    //String fileName = createPDF(applyPerson, tid);
   /* SysUploadFile suf = new SysUploadFile();
    suf.set("filename", "ABEC-IncomeCertificate.pdf");
    suf.set("savename", "");
    suf.set("filepath", savePath);
    suf.save();
    OnlineProof onlineProof = new OnlineProof();
    onlineProof.set("id", getPara("id"));
    onlineProof.set("upload_id", suf.get("id"));
    onlineProof.update();*/
    File htmlFile;
    File htmlMidFile = null;
    //FileReader reader = null;
    InputStreamReader reader = null;
    FileWriter writer = null;
    String url = this.getRequest().getServletContext().getRealPath("/");
    String proofType = getPara("proofType");
    String fileName = "";
    String country = getPara("country");
    String travelMode = getPara("travelMode");
    String fileUrl = url + "/resources/kong/";
    switch (proofType) {
        case "1":
            fileUrl += "Working Certificate.html";
            fileName += "Working Certificate";
            break;
        case "2":
            fileUrl += "Working and Income Certificate.html";
            fileName += "Working and Income Certificate";
            break;
        case "3":
            fileUrl += "Certificate for Loan.html";
            fileName += "Certificate for Loan";
            break;
        case "4":
            if ("1".equals(travelMode)) {
                if ("1".equals(country) || "2".equals(country) || "3".equals(country)
                        || "5".equals(country) || "6".equals(country) || "7".equals(country)) {
                    fileUrl += "Certificate for Visa - EU&Business.html";
                } else if ("8".equals(country)) {
                    fileUrl += "Certificate for Visa - UK&Business.html";
                } else if ("9".equals(country) || "10".equals(country) || "12".equals(country)) {
                    fileUrl += "Certificate for Visa - Asia&Business.html";
                } else if ("11".equals(country)) {
                    fileUrl += "Certificate for Visa - Taiwan&Businessl.html";
                }
            } else if ("2".equals(travelMode)) {
                if ("1".equals(country) || "2".equals(country) || "3".equals(country)
                        || "5".equals(country) || "6".equals(country) || "7".equals(country)) {
                    fileUrl += "Certificate for Visa - EU&Personal.html";
                } else if ("8".equals(country)) {
                    fileUrl += "Certificate for Visa - UK&Personal.html";
                } else if ("9".equals(country) || "10".equals(country) || "12".equals(country)) {
                    fileUrl += "Certificate for Visa - Asia&Personal.html";
                } else if ("11".equals(country)) {
                    fileUrl += "Certificate for Visa - Taiwan&Personal.html";
                }
            }
            fileName += "Certificate for Visa";
            break;
        case "5":
            fileUrl += "";
            fileName += "Certificate";
            break;
    }
    htmlFile = new File(fileUrl);// 指定要读取的文件

    try {

        reader = new InputStreamReader(new FileInputStream(htmlFile), "utf-8");
        //reader = new FileReader(htmlFile);
        // 获取该文件的输入流
        char[] bb = new char[1024];// 用来保存每次读取到的字符
        String str = "";// 用来将每次读取到的字符拼接,当然使用StringBuffer类更好
        int n;// 每次读取到的字符长度
        while ((n = reader.read(bb)) != -1) {
            str += (new String(bb, 0, n));
        }
        System.out.println(str);
        //替换HTML参数
        //str = str.replace("@Name_Cn@","test");

        htmlMidFile = new File(savePath + fileName+ ".html");// 要写入的文本文件

        if (!htmlMidFile.exists()) {// 如果文件不存在,则创建该文件
            htmlMidFile.createNewFile();
        }
        writer = new FileWriter(htmlMidFile);// 获取该文件的输出流
        writer.write(str);// 写内容
        writer.flush(); // 清空缓冲区,立即将输出流里的内容写到文件里
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            try {
                writer.close(); // 关闭输出流,施放资源
            } catch (IOException ignored) {
            }
        }
        if (reader != null) {
            try {
                reader.close(); // 关闭输入流,释放连接
            } catch (IOException ignored) {
            }
        }
    }
    //下载PDF文档
    Document document;
    // 将html转为pdf
    document = new Document(PageSize.A4, 36, 36, 108, 108);
    // 解决中文支持问题
    PdfWriter pdfwriter;
    try {
        pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(savePath + fileName +  ".pdf"));
        document.open();
        String path = this.getRequest().getServletContext().getRealPath("/");
        Image img = Image.getInstance(path + "/img/ABEC.png");
        img.setAbsolutePosition(0, 0);
        img.scaleToFit(900, 860);
        document.add(img);
        //加载HTML
        XMLWorkerHelper.getInstance()
                .parseXHtml(
                        pdfwriter,
                        document,
                        new InputStreamReader(new FileInputStream(htmlFile), "utf-8"));
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
    } finally {
        document.close();
        // 删除临时html
        htmlMidFile.delete();
    }
    //下载PDF
    try {
        FileOperateUtil.downloadx(getRequest(), getResponse(),
                savePath + fileName + ".pdf", "application/octet-stream",
                fileName + ".pdf");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Input/output (I/O) is not a sexy subject, but it’s an important part of non-trivial applications. This book introduces you to most of Java’s I/O capabilities as of Java 8 update 51. Chapter 1 presents a broad overview of I/O in terms of Java’s classic I/O, New I/O (NIO), and NIO.2 categories. You learn what each category offers in terms of its capabilities, and you also learn about concepts such as paths and Direct Memory Access. Chapters 2 through 5 cover classic I/O APIs. You learn about the File and RandomAccessFile classes along with streams (including object serialization and externalization) and writers/readers. Chapters 6 through 11 focus on NIO. You explore buffers, channels, selectors, regular expressions, charsets, and formatters. (Formatters were not introduced with the other NIO types in Java 1.4 because they depend on the variable arguments capability that was introduced in Java 5.) NIO is missing several features, which were subsequently provided by NIO.2. Chapters 12 through 14 cover NIO.2’s improved file system interface, asynchronous I/O, and the completion of socket channel functionality. Each chapter ends with assorted exercises that are designed to help you master its content. Along with long answers and true/false questions, you are often confronted with programming exercises. Appendix A provides the answers and solutions. Appendix B provides a tutorial on sockets and network interfaces. Although not directly related to classic I/O, NIO, and NIO.2, they leverage I/O capabilities and are mentioned elsewhere in this book.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柠檬不萌c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值