借助PD4ML将HTML转化成RTF或者PDF

借助PD4ML将HTML转化成RTF或者PDF

需要借助pd4mljar包

代码HTMLtoRTF


`import java.awt.Insets;
import java.io.File;
import java.io.IOException;  
import java.net.MalformedURLException;  
import java.net.URL;  
import java.security.InvalidParameterException;   
import org.zefer.pd4ml.PD4Constants;  
import org.zefer.pd4ml.PD4ML;
import junit.framework.Test;  
  
public class HTMLtoRTF {  
	 //页面边距  上下左右
    protected int topValue = 10;  
    protected int leftValue = 20;  
    protected int rightValue = 10;  
      		  int bottomValue = 10;  
    protected int userSpaceWidth = 1300;  
  
    public static void main(String[] args) {  
        try {  
        	HTMLtoRTF jt = new HTMLtoRTF();  
        	
            jt.doConversion("file:///C:/Users/111/Desktop/test.html", "C:\\Users\\111\\Desktop\\test.rtf");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }   
   
    public void doConversion( String url, String outputPath )   
                throws InvalidParameterException, MalformedURLException, IOException {  
        File output = new File(outputPath);
        //写入文件
        java.io.FileOutputStream fos = new java.io.FileOutputStream(output);  
  
        PD4ML pd4ml = new PD4ML();  
              
        pd4ml.setHtmlWidth(userSpaceWidth); // set frame width of "virtual web browser"   
              
        // A4格式  
        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));   
               
        // 定义边距
        pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
       
               
        //字体添加
        String classPath = Test.class.getResource("/")+"fonts";
        pd4ml.useTTF(classPath, true);
        //默认字体
        //  pd4ml.setDefaultTTFs("Kingsoft", "", "");
        
        // 强制生成RTF  
        pd4ml.outputFormat(PD4Constants.RTF_WMF);  
      
        //  从URL到RTF文件的实际文档转换 
        pd4ml.render(new URL(url), fos); 
        fos.close();  
              
        System.out.println( outputPath + "\ndone." );  
    } 
   
}  

代码HTML to PDF


import java.awt.Insets;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.security.InvalidParameterException;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
 
public class HtmltoPDF {
	protected int topValue = 10;
	protected int leftValue = 20;
	protected int rightValue = 10;
	protected int bottomValue = 10; 
	protected int userSpaceWidth = 1300;
 
	public static void main(String[] args) {
		try {
			HtmltoPDF jt = new HtmltoPDF();
			String html = readFile("C:\\Users\\111\\Desktop\\html2.html", "UTF-8");
			jt.doConversion2(html, "C:\\Users\\111\\Desktop\\pd4ml.pdf");
		} catch (Exception e) {
			e.printStackTrace();	 
		}
	}
 
	public void doConversion2( String htmlDocument, String outputPath ) 
				throws InvalidParameterException, MalformedURLException, IOException {
 
		PD4ML pd4ml = new PD4ML();
			 
		pd4ml.setHtmlWidth(userSpaceWidth); // set frame width of "virtual web browser" 
			
		// choose target paper format
		pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4)); 
			
		// define PDF page margins
		pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue)); 
  
		// source HTML document also may have margins, could be suppressed this way 
		// (PD4ML *Pro* feature):
		pd4ml.addStyle("BODY {margin: 0}", true);
			
		// If built-in basic PDF fonts are not sufficient or 
		// if you need to output non-Latin texts, TTF embedding feature should help 
		// (PD4ML *Pro*)
		
 
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		// actual document conversion from HTML string to byte array
		pd4ml.render(new StringReader(htmlDocument), baos); 
		// if the HTML has relative references to images etc, 
		// use render() method with baseDirectory parameter instead
		baos.close();
		
		System.out.println( "resulting PDF size: " + baos.size() + " bytes" );
		// in Web scenarios it is a good idea to send the size with 
		// "Content-length" HTTP header
 
		File output = new File(outputPath);
		java.io.FileOutputStream fos = new java.io.FileOutputStream(output);
		fos.write( baos.toByteArray() );
		fos.close();
		
		System.out.println( outputPath + "\ndone." );
	}
	
	private final static String readFile( String path, String encoding ) throws IOException {
 
		File f = new File( path );
		FileInputStream is = new FileInputStream(f);
		BufferedInputStream bis = new BufferedInputStream(is);
		
		ByteArrayOutputStream fos = new ByteArrayOutputStream();
		byte buffer[] = new byte[2048];
 
		int read;
		do {
			read = is.read(buffer, 0, buffer.length);
			if (read > 0) { 
				fos.write(buffer, 0, read); 
			}
		} while (read > -1);
 
		fos.close();
		bis.close();
		is.close(); 
 
		return fos.toString(encoding);
	}
}


解决中文乱码问题

  • 新建一个文件夹,把字体文件(.ttf)复制进去,再把pd4ml.jar复制进去,此时还没有配置文件, 在fonts文件夹里打开cmd命令行:
    java -jar pd4ml-310b10.jar -configure.fonts …/fonts …/font

  • 将生成的pd4fonts.properties文件中最后一行
    font.dir.location=…/fonts 删除,不删除执行会报错找不到配置文件

  • 设置字体:

    将上面字体和生成的文件放到resources下的fonts文件夹里面。

String classPath = Test.class.getResource("/")+"fonts";
        pd4ml.useTTF(classPath, true);

相关jar包和资源地址:
https://download.csdn.net/download/qq_46432776/85057047

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

scblue_club

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

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

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

打赏作者

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

抵扣说明:

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

余额充值