java html生成PDF,并打印

折月煮酒 提交于 2020-11-18 18:23:30
import java.io.File;
import java.io.FileOutputStream;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
import org.zefer.pd4ml.PD4PageMark;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;

public void createPDF(String printHtml, File pdffile) throws InvalidParameterException, IOException{
    StringBuffer html = new StringBuffer();
    html.append("<style>")
    .append(" .pageBreak{page-break-after: always; }")
    .append("</style>");
    html.append(printHtml);
    html.append("<pd4ml:page.break>");
    StringReader strReader = new StringReader(getHtml(html).toString());
    FileOutputStream fos = new FileOutputStream(pdffile);
    PD4ML pd4ml = new PD4ML();
    pd4ml.setPageInsets(new Insets(5, 5, 5, 5));
    pd4ml.setHtmlWidth(700);
    pd4ml.setPageSize(PD4Constants.A4);
    pd4ml.useTTF("file:"+System.getProperty("ofbiz.home")+"/hot-deploy/heluoexam/webapp/exam/fonts", true);
    PD4PageMark footer = new PD4PageMark();  
    footer.setHtmlTemplate("<div style=\"font-family:宋体;font-size:8px;text-align:center;width:100%\">第 $[page] 页  共 $[total] 页</div>");  
    footer.setAreaHeight(-1);
    pd4ml.setPageFooter(footer);
    pd4ml.render(strReader, fos);
}

public StringBuffer getHtml(Object in){
    StringBuffer html = new StringBuffer();
    html.append("<html>")
        .append("<head>")
        .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
        .append("<style>")
        .append("BODY {PADDING-BOTTOM: 0px; LINE-HEIGHT: 1.5; FONT-STYLE: normal; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: #333; FONT-SIZE: 13px; FONT-WEIGHT: normal; PADDING-TOP: 0px}")
        .append("table{border:1px solid #000;}")
        .append("table td{border:1px solid #000;}")
        .append("</style>")
        .append("</head>")
        .append("<body>");
    html.append(in);
    html.append("</body></html>");
    return html;
}

private void PrintPDF(File pdFile) {
    //构建打印请求属性集
    HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
    //设置打印格式,如未确定类型,选择autosense
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
    //查找所有的可用的打印服务
//    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
    //定位默认的打印服务
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
    //显示打印对话框
//    PrintService service = ServiceUI.printDialog(null, 200, 200, printService,defaultService, flavor, pras);
    if(defaultService != null){
        try {
            DocPrintJob job = defaultService.createPrintJob();//创建打印作业
            FileInputStream fis = new FileInputStream(pdFile);//构造待打印的文件流
            DocAttributeSet das = new HashDocAttributeSet();
            Doc doc = new SimpleDoc(fis, flavor, das);
            job.print(doc, pras);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!