java把html转pdf

java 926 2019-04-19 10:13

所要的依赖



    
    com.openhtmltopdf
    openhtmltopdf-core
    ${openhtml.version}



    
    com.openhtmltopdf
    openhtmltopdf-pdfbox
    ${openhtml.version}



    
    com.openhtmltopdf
    openhtmltopdf-java2d
    ${openhtml.version}



    
    com.openhtmltopdf
    openhtmltopdf-rtl-support
    ${openhtml.version}



    
    com.openhtmltopdf
    openhtmltopdf-jsoup-dom-converter
    ${openhtml.version}



    
    com.openhtmltopdf
    openhtmltopdf-slf4j
    ${openhtml.version}



    
    com.openhtmltopdf
    openhtmltopdf-log4j
    ${openhtml.version}



    
    com.openhtmltopdf
    openhtmltopdf-svg-support
    ${openhtml.version}



    
    
    com.openhtmltopdf
    openhtmltopdf-mathml-support
    ${openhtml.version}

 

import com.openhtmltopdf.bidi.support.ICUBidiReorderer;
import com.openhtmltopdf.bidi.support.ICUBidiSplitter;
import com.openhtmltopdf.extend.FSSupplier;
import com.openhtmltopdf.mathmlsupport.MathMLDrawer;
import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder;
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.svgsupport.BatikSVGDrawer;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;

import com.openhtmltopdf.pdfboxout.PdfRendererBuilder.PdfAConformance;


public class PdfUtils {

    public static void getHtmlToPdf(HttpServletResponse response, String htmlText, String path, boolean isDowan) {
        try {
            if (isDowan) {
                // 下载文件的默认名称
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(path + new Date().getTime() + ".pdf", "utf-8"));
            }
            response.setHeader("Content-type", "application/pdf");
            ServletOutputStream outputStream = response.getOutputStream();
            renderPDF(path, htmlText, PdfAConformance.NONE, outputStream);
        } catch (Exception e) {
            //e.printStackTrace();

        }
    }


    private static void renderPDF(String path, String html, PdfRendererBuilder.PdfAConformance pdfaConformance, OutputStream outputStream)
            throws Exception {
        try {
            //  PdfAConformance conform = PdfAConformance.PDFA_1_A;
            PdfRendererBuilder builder = new PdfRendererBuilder();
            builder.useFastMode();
            // builder.usePdfVersion(conform.getPart() == 1 ? 1.4f : 1.5f);
            // builder.usePdfAConformance(conform);
            final String os = System.getProperty("os.name");
            System.out.println(os);
            //builder.useFastMode();
            //下面这个方法是要自己指定 字体文件   不然转出的pdf文件中 中文会变成####
            builder.useFont(new FSSupplier() {
                @Override
                public InputStream supply() {
                    try {
                        //指定 字体文件


                        if (os.toLowerCase().startsWith("win")) {
                            String s = PdfUtils.class.getResource("/font/simsun.ttf").toString();
                            if (s != null) {
                                s = s.substring(6);
                            }
                            return new FileInputStream(s);

                        } else {
                            return new FileInputStream("/opt/staticsource/pdf/simsun.ttf");

                        }

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
            }, "simsun", 400, BaseRendererBuilder.FontStyle.NORMAL, true); //第二个参数 一定要和文件名一样!!作用在html页面上
            /**
             * gps    350
             * bol    360
             * lun    365
             */
            switch (path) {
                case "gps":
                    builder.useDefaultPageSize(250.4F, 350F, null);
                    break;
                case "bol":
                    builder.useDefaultPageSize(252.4F, 360F, null);
                    break;
                case "lun":
                    builder.useDefaultPageSize(252.4F, 365F, null);
                    break;
            }

            builder.useUnicodeBidiSplitter(new ICUBidiSplitter.ICUBidiSplitterFactory());
            builder.useUnicodeBidiReorderer(new ICUBidiReorderer());
            builder.defaultTextDirection(BaseRendererBuilder.TextDirection.LTR);
            builder.useSVGDrawer(new BatikSVGDrawer());
            builder.useMathMLDrawer(new MathMLDrawer());
            // builder.addDOMMutator(LaTeXDOMMutator.INSTANCE);
            //   builder.useObjectDrawerFactory(buildObjectDrawerFactory());
            builder.usePdfAConformance(pdfaConformance);
            if (os.toLowerCase().startsWith("win")) {
                builder.withHtmlContent(html, "c://");

            } else {
                builder.withHtmlContent(html, "/opt");
            }

            builder.toStream(outputStream);
            builder.run();
        } finally {
            outputStream.close();
        }
    }


}

 

 

 

 

 

文章评论