根据html生成pdf并解决标点出现在行首的问题

本文介绍了使用itextpdf和wkhtmlpdf库进行HTML转PDF的方法,特别关注了中文显示和标点符号在行首的问题,以及如何通过配置字体解决这些问题。
摘要由CSDN通过智能技术生成
1.使用itextpdf
  • 可以顺利生成pdf但需要搭配字体否则会出现中文不显示问题 , 并且存在部分字体会对行首符号显示问题(字号变大加粗等)
  • pdf字体首先根据html中设置 , html中设置的字体不存在则使用Java代码中设置的
    /**
     * html转pdf
     * 存在标点符号在首行的问题
     * @param htmlPath
     * @param pdfPath
     * @throws Exception
     */
    public static void htmlToPdf(String htmlPath, String pdfPath) throws Exception {
        //创建对象生成pdf文件
        PdfDocument pdf = new PdfDocument(new PdfWriter(pdfPath));
        //指定pdf大小A4
        Document document = new Document(pdf, PageSize.A4);
        //读取HTML文件
        InputStream htmlStream = new FileInputStream(htmlPath);
        //创建一个ConverterProperties对象,用于设置转换属性
        ConverterProperties converterProperties = new ConverterProperties();
        //解决中文无法显示
        //创建一个DefaultFontProvider对象,用于提供字体。
        FontProvider fontProvider = new DefaultFontProvider();
        //其余字体存在书名号在开头时变形的问题
FontProgramFactory.createFont("C:\\Windows\\Fonts\\STKAITI.TTF");
        //创建一个FontProgram对象,加载simkai.ttf字体。
        FontProgram fontProgram = FontProgramFactory.createFont("C:\\Windows\\Fonts\\simkai.ttf");
        //将simkai.ttf字体添加到字体提供者中。
        fontProvider.addFont(fontProgram);

        converterProperties.setFontProvider(fontProvider);
        //使用HtmlConverter类的convertToPdf方法将HTML文件转换为PDF文件。
        HtmlConverter.convertToPdf(htmlStream, pdf, converterProperties);
        document.close();
    }
2.使用wkhtmlpdf
    //    wkhtmltopdf在系统中的路径
    public static final String toPdfTool = "E:\\WkHtmlToPdf\\bin\\wkhtmltopdf.exe";

    /**
     * html转pdf
     * 解决标点符号出现在首行的内容
     * pdf字体存依靠html
     * @param srcPath  html路径,可以是硬盘上的路径,也可以是网络路径
     * @param destPath pdf保存路径
     * @return 转换成功返回true
     */
    public static boolean convert(String srcPath, String destPath,String toPdfTool){
        File file = new File(destPath);
        File parent = file.getParentFile();
        //如果pdf保存路径不存在,则创建路径
        if(!parent.exists()){
            parent.mkdirs();
        }
        StringBuilder cmd = new StringBuilder();
        cmd.append(toPdfTool);
        cmd.append(" ");
//        cmd.append(" --header-line");//页眉下面的线
//        cmd.append(" --margin-top 3cm ");//设置页面上边距 (default 10mm)
        // cmd.append(" --header-html file:///"+WebUtil.getServletContext().getRealPath("")+FileUtil.convertSystemFilePath("\\style\\pdf\\head.html"));// (添加一个HTML页眉,后面是网址)
//        cmd.append(" --header-spacing 5 ");// (设置页眉和内容的距离,默认0)
        //cmd.append(" --footer-center (设置在中心位置的页脚内容)");//设置在中心位置的页脚内容
        //cmd.append(" --footer-html file:///"+WebUtil.getServletContext().getRealPath("")+FileUtil.convertSystemFilePath("\\style\\pdf\\foter.html"));// (添加一个HTML页脚,后面是网址)
//        cmd.append(" --footer-line");//* 显示一条线在页脚内容上)
//        cmd.append(" --footer-spacing 5 ");// (设置页脚和内容的距离)
        cmd.append(srcPath);
        cmd.append(" ");
        cmd.append(destPath);
        boolean result = true;
        try{
            Process proc = Runtime.getRuntime().exec(cmd.toString());
            HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
            HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream());
            error.start();
            output.start();
            proc.waitFor();
        }catch(Exception e){
            result = false;
            e.printStackTrace();
        }

        return result;
    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值