java html生成PDF,並打印


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();
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM