重庆小潘seo博客

当前位置:首页 > 重庆网络营销 > 小潘杂谈 >

小潘杂谈

示例小程序生成海报(java后端)

时间:2020-09-06 04:30:07 作者:重庆seo小潘 来源:
【相关学习推荐:小程序开发教程】实现方案实现步骤安装环境官网地址:https://wkhtmltopdf.org/ windows: 下载安装包安装即可 linux:下载对应的安装包 ,还需安装对应中文字体(phantomjs 也需要安装字体),html中需要声明引用相关代码 利用java 执行命令

示例小程序生成海报(java后端)

【相关学习推荐:小程序开发教程】实现方案实现步骤安装环境官网地址:https://wkhtmltopdf.org/

windows: 下载安装包安装即可

linux:下载对应的安装包 ,还需安装对应中文字体(phantomjs 也需要安装字体),html中需要声明引用相关代码 利用java 执行命令 调用wkhtmltoImage 设置相关参数,具体参数查看wkhtmltoImage 命令提示package com.yumingzhu.wxweb.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** * @Description* @Author yumigzhu * @Date 2020/7/22 20:12 */public class CustomWKHtmlToPdfUtil {private static String tempPath = "C:/apps/tmpFile";// 图片保存目录public static String getCommand(String htmlToImage, String sourceFilePath, String targetFilePath) {//--quality 设置为50 是比较合适的, 默认的94 可能会导致图片文件过大ProcessBuilder pb = new ProcessBuilder(htmlToImage, "--crop-w", "800", "--width", "800","--quality", "50",sourceFilePath, targetFilePath);Process process;try {process = pb.start();//注意,调用process.getErrorStream()而不是process.getInputStream()BufferedReader errStreamReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));System.out.println("read errstreamreader");String line = null;line = errStreamReader.readLine();while (line != null) {System.out.println(line);line = errStreamReader.readLine();}process.destroy();System.out.println("destroyed process");} catch (IOException e) {e.printStackTrace();}return targetFilePath;}public static void main(String[] args) throws Exception {String imagePath = tempPath + "/" + System.currentTimeMillis() + ".png";//图片路径System.out.println(imagePath);String htmlToImage = "E:\softwareAPP\wkhtmltopdf\bin\wkhtmltoimage.exe";CustomWKHtmlToPdfUtil.getCommand(htmlToImage,"file:///G:/share/text_none_title_share/index.html",imagePath);System.out.println("执行完成");}}踩坑记录如果html页面设置的宽高比较小, 这样截出来的图片也会比较小,比较模糊,,增大html 的宽高,可以使图片更清晰,这样会导致截出来的图片文件更大,这样用户在小程序下载过程会更慢,这里需要自己权衡

wkhtmlImage 对 css3linear-gradient不支持,不能使用样式下划线,可以考虑使用图片代替

中文字体需要声明引用,才能生效

以上就是示例小程序生成海报(java后端)的详细内容,更多请关注小潘博客其它相关文章!