String to jpg

/**
     * 将htmlCode转换为jpg文件
     * @param html     其中body体必须包含字体样式设置,否则无法识别汉字。
     *                 字体样式设置举例:style='font-family:SimSun'
     * @param fileName
     */
    public void htmlCodeToJpgAndUpload(String html, String fileName) throws Exception {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ByteArrayOutputStream byteArrayOutputStreamTemp = null;
        AttachmentInfo attachmentInfo = null;
        Stopwatch stopwatch = Stopwatch.createStarted();
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            byteArrayOutputStreamTemp = new ByteArrayOutputStream();
            ITextRenderer renderer = new ITextRenderer();
            ITextFontResolver fontResolver = renderer.getFontResolver();
            fontResolver.addFont(FONTS_PATH, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            renderer.setDocumentFromString(html);
            renderer.layout();
            renderer.createPDF(byteArrayOutputStreamTemp);

            log.info("html to pdf 字节流转换成功,开始转换JPG,fileName = {}", fileName);
            Document document = new Document();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStreamTemp.toByteArray());
            document.setInputStream(byteArrayInputStream, "");
            /*
            缩放比例(大图)
             */
            float scale = 1f;
            /*
            旋转角度
             */
            float rotation = 0f;
            for (int i = 0; i < document.getNumberOfPages(); i++) {
                BufferedImage image = (BufferedImage) document.getPageImage(i,
                        GraphicsRenderingHints.SCREEN,
                        org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX,
                        rotation, scale);
                RenderedImage rendImage = image;
                ImageIO.write(rendImage, "png", byteArrayOutputStream);
                image.flush();
            }
            document.dispose();
            log.info("pdf to jpg 成功,开始上传服务器,fileName = {}", fileName);
            attachmentInfo = attachmentsService.saveFile(byteArrayOutputStream.toByteArray(), fileName);
            log.info("文件 {} 上传成功 ", fileName);
        } catch (FileNotFoundException e) {
            log.error("{}字体文件找不到", FONTS_PATH, e);
            throw e;
        } catch (Exception e) {

            log.error("htmlCodeToPdfAndUpload 转换异常,html = {} ,fileName = {}", html, fileName, e);
            throw e;
        } finally {
            if(byteArrayOutputStream != null){
                byteArrayOutputStream.close();
            }
            long spend = stopwatch.stop().elapsed(TimeUnit.MILLISECONDS);
            log.info("htmlCodeToPdfAndUpload() html = {} ,pdfName = {},spend : {}ms", html, fileName, spend);

        }

    }
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

    public String getHtml(String str) throws ConverFormatException {
        String reportHtml = null;
        EtermJson etermJson = new EtermJson().setEtermStr(str);
        try {
            Template template = freeMarkerConfiguration.getTemplate("refundEnvience.ftl");
            reportHtml = FreeMarkerTemplateUtils.processTemplateIntoString(template, etermJson);
        } catch (Exception e) {
            logger.error("转换html失败:{}", JsonUtils.Object2Json(str));
            throw new ConverFormatException("Json to html 异常");
        }
        return reportHtml;
    }
<!--itext 转pdf-->
            <dependency>
                <groupId>org.xhtmlrenderer</groupId>
                <artifactId>flying-saucer-pdf</artifactId>
                <version>${org.xhtmlrenderer.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.bouncycastle</groupId>
                        <artifactId>bcprov-jdk14</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.bouncycastle</groupId>
                        <artifactId>bctsp-jdk14</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.bouncycastle</groupId>
                        <artifactId>bcmail-jdk14</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <dependency>
                <groupId>com.itextpdf.tool</groupId>
                <artifactId>xmlworker</artifactId>
                <version>${com.itextpdf.tool.version}</version>
            </dependency>
            <!-- end -->

            <!-- ant -->
            <dependency>
                <groupId>org.apache.ant</groupId>
                <artifactId>ant</artifactId>
                <version>1.9.7</version>
            </dependency>

            <!--pdf to png-->
            <dependency>
                <groupId>org.icepdf.os</groupId>
                <artifactId>icepdf-viewer</artifactId>
                <version>6.2.2</version>
                <exclusions>
                    <exclusion>
                        <groupId>javax.media</groupId>
                        <artifactId>jai_core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.bouncycastle</groupId>
                        <artifactId>bcprov-jdk15on</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.bouncycastle</groupId>
                        <artifactId>bcprov-ext-jdk15on</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.bouncycastle</groupId>
                        <artifactId>bcpkix-jdk15on</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>e-iceblue</groupId>
                <artifactId>spire.pdf.free</artifactId>
                <version>2.2.2</version>
            </dependency>


    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>

 

 

代码只供参考。提供一个思路

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值