Freemaker 模板生成html文件带barcode4j条形码并转PDF

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chenaini119/article/details/52043131

前面野了三篇博客,现在直接结合在一起做一个实例

首先生成条形码并且放入html转换成pdf下载下来,将所有功能集合在一起
1.条形码

String barcode=BarCodeUtil.create39Code(“A123456”);

实现方法

public static String create39Code(String code) {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        try {
            // Create the barcode bean
            Code39Bean bean = new Code39Bean();
            final int dpi = 140;
            // Configure the barcode generator
            bean.setModuleWidth(UnitConv.in2mm(1.0f / 140)); // makes the narrow
            bean.setHeight(13);
            bean.setWideFactor(3);// width exactly one pixel
            bean.doQuietZone(false);
            bean.setQuietZone(2);// 两边空白区
            // bean.setMsgPosition(HumanReadablePlacement.HRP_NONE);
            bean.setFontSize(5);
            bean.setDisplayStartStop(true);
            // Set up the canvas provider for monochrome JPEG output
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(baos, "image/png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
            // Generate the barcode
            bean.generateBarcode(canvas, code);
            // Signal end of generation
            canvas.finish();
        } catch (Exception e) {
            logger.error("单号:{}生成条形码出错",code,e);
        }
        String base64Img=Base64.byteArrayToBase64(baos.toByteArray());
        return base64Img;
    }

2.模板赋值

    String html=FreemarkerUtil.getHtml(String base64Img);

实现方法:

public static String getHtml(String base64Img) throws Exception {
        // 创建插值的Map
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("users", base64Img);
        StringWriter stringWriter = new StringWriter();
        BufferedWriter writer = new BufferedWriter(stringWriter);
        template.setEncoding("UTF-8");
        // 执行插值,并输出到指定的输出流中
        template.process(map, writer);
        String html = stringWriter.toString();
        writer.flush();
        writer.close();
        return html;
    }

3.模板转PDF

ByteArrayOutputStream baos=FlyingSuacerUtil.htmlToPdf(String html);

实现方法:

public class FlyingSuacerUtil {
    public static ByteArrayOutputStream htmlToPdf(String html) throws Exception { 
        ByteArrayOutputStream baos=new ByteArrayOutputStream();   
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(html);
        // 解决中文支持问题     
        ITextFontResolver fontResolver = renderer.getFontResolver();     
        String env=SystemConstant.getProperty("spring.profiles.active");
        String os = System.getProperty("os.name");
        String fontPath=SystemConstant.getProperty("font.simsun.path");
        if(env.equals(Constants.ENV_DEVELOPMENT)&&os.startsWith(Constants.OS_WINDOWS)){
            fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        }else{
            fontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        }
        renderer.layout();     
        renderer.createPDF(baos); 
        return baos;
    }     

最后是PDF输出流,将输出流传人PDF文件就好了。

public static void download(HttpServletResponse response, ByteArrayOutputStream baos, String fileName) throws IOException {
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"ISO-8859-1"));
        response.addHeader("Content-Length", String.valueOf(baos.size()));
        OutputStream os = response.getOutputStream();
        os.write(baos.toByteArray());
        os.close();
        baos.close();
    }

或者:

File outFile = new File("D:\\index.pdf");
            try {
            outFile.createNewFile();
            OutputStream os = new FileOutputStream(outFile);
                    os.write(baos.toByteArray());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                os.flush();
                os.close();
            }
    }
展开阅读全文

没有更多推荐了,返回首页