HTML文件转PDF

HTML文件转PDF

  1. 利用IText将HTML文件转为pdf
    pom文件
<dependency>
        <groupId>org.xhtmlrenderer</groupId>
        <artifactId>core-renderer</artifactId>
        <version>R8</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.15</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.1</version>
    </dependency>
    <dependency>
      <groupId>com.itextpdf.tool</groupId>
  	  <artifactId>xmlworker</artifactId>
   	 <version>5.5.9</version>
    </dependency>
public static void main(String[] args) {
        StringBuilder strline = new StringBuilder("");
        File fin = new File("C:\\Users\\about.html");
        try (RandomAccessFile accessFile = new RandomAccessFile(fin, "r");
             FileChannel fcin = accessFile.getChannel();
        ) {
            Charset charset = Charset.forName("UTF-8");
            int bufSize = 100000;
            ByteBuffer rBuffer = ByteBuffer.allocate(bufSize);
            String enterStr = "\n";
            byte[] bs = new byte[bufSize];

            StringBuilder strBuf = new StringBuilder("");
            while (fcin.read(rBuffer) != -1) {
                int rSize = rBuffer.position();
                rBuffer.rewind();
                rBuffer.get(bs);
                rBuffer.clear();
                String tempString = new String(bs, 0, rSize, charset);
                tempString = tempString.replaceAll("\r", "");

                int fromIndex = 0;
                int endIndex = 0;
                while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
                    String line = tempString.substring(fromIndex, endIndex);
                    line = strBuf.toString() + line;
                    strline.append(line.trim());
                    strBuf.delete(0, strBuf.length());
                    fromIndex = endIndex + 1;
                }
                if (rSize > tempString.length()) {
                    strline.append(tempString.substring(fromIndex, tempString.length()));
                    strBuf.append(tempString.substring(fromIndex, tempString.length()));
                } else {
                    strline.append(tempString.substring(fromIndex, rSize));
                    strBuf.append(tempString.substring(fromIndex, rSize));
                }
            }
            String htmlString = "";
            htmlString = strline.toString().replaceAll("\"", "'").replaceAll("<style>", "<style>body{font-family:SimSun;font-size:14px;}");    
            OutputStream os = new FileOutputStream("C:\\Users\\about.pdf");    //生成PDF文件的路径
            ITextRenderer renderer = new ITextRenderer();
            ITextFontResolver font = renderer.getFontResolver();
            font.addFont("C:/WINDOWS/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            renderer.setDocumentFromString(htmlString.toString());
            renderer.layout();
            renderer.createPDF(os);
            renderer.finishPDF();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
  1. 使用html2pdf
 <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>4.0.2</version>
        </dependency>
public static void main(String[] args) {
        File file=new File("about.html");
        StringBuffer buffer=new StringBuffer();
        try{
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            while(bufferedReader.ready()){
                buffer.append(bufferedReader.readLine());
            }
            bufferedReader.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        String html =buffer.toString();
        try(FileOutputStream outputStream = new FileOutputStream("D:\\table.pdf")){

            Long start = Calendar.getInstance().getTimeInMillis();

            html = html.replace("&nbsp;", " ");
            html = html.replace("&ndash;", "–");
            html = html.replace("&mdash;", "—");
            html = html.replace("&lsquo;", "‘");
            html = html.replace("&rsquo;", "’");
            html = html.replace("&sbquo;", "‚");
            html = html.replace("&ldquo;", "“");
            html = html.replace("&rdquo;", "”");
            html = html.replace("&bdquo;", "„");
            html = html.replace("&prime;", "′");
            html = html.replace("&Prime;", "″");
            html = html.replace("&lsaquo;", "‹");
            html = html.replace("&rsaquo;", "›");
            html = html.replace("&oline;", "‾");

            ConverterProperties converterProperties = new ConverterProperties();
            FontProvider provider = new FontProvider();
            provider.addFont("C:/WINDOWS/Fonts/STXIHEI.TTF");
            converterProperties.setFontProvider(provider);
            HtmlConverter.convertToPdf(html,outputStream,converterProperties);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值