定制开发网站/自己做网站流程
需求
用户可以选择以长图的形式分享本网页
方法
- wkhtmltopdf
- wkhtmltopdf url file
- wkhtmltoimage url file
- java
- Runtime.getRuntime().exec()
下载
直接去官网下载对应的版本:官网
命令行使用WK
> wkhtmltopdf https://www.nowcoder.com /opt/project/java/mycommunity-pdfs/1.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done> wkhtmltoimage https://www.nowcoder.com /opt/project/java/mycommunity-images/1.png
Loading page (1/2)
Rendering (2/2)
Done// 上面那条命令生成的长图太大了,可以使用下面这条:以75%的质量输出
> wkhtmltoimage --quality 75 https://www.nowcoder.com /opt/project/java/mycommunity-images/1.png
Loading page (1/2)
Rendering (2/2)
Done
在java中调用
application.properties:
mycommunity.path.domain=http://localhost:8080
server.servlet.context-path=/myCommunity
# WK
wk.image.command=/usr/local/bin/wkhtmltoimage
wk.image.storage=/opt/project/java/mycommunity-images/
WK配置类,在每次程序开始运行时自动生成存放图像的文件夹
@Configuration
public class WkConfig {private static final Logger logger = LoggerFactory.getLogger(WkConfig.class);@Value("${wk.image.storage}")private String wkImageStorage;@PostConstructpublic void init(){// create the dic about WKimageFile file = new File(wkImageStorage);if(!file.exists()){file.mkdir();logger.info("create the dictionary of WKimage: " + wkImageStorage);}}
}
调用WK的控制层,因为生成长图比较耗时,所以使用异步操作,在用户操作时调用Kafka的生产者生成事件,通知消费者:
@Controller
public class ShareController implements CommunityConstant {private static final Logger logger = LoggerFactory.getLogger(ShareController.class);@Autowiredprivate EventProducer eventProducer;@Value("${mycommunity.path.domain}")private String domain;@Value("${server.servlet.context-path}")private String contextPath;@Value("${wk.image.storage}")private String wkImageStorage;@GetMapping(path = "/share")@ResponseBodypublic String share(String htmlUrl){// generate the file nameString fileName = CommunityUtil.generateUUID();// Asynchronous generation long picEvent event = new Event().setTopic(TOPIC_SHARE).setData("htmlUrl", htmlUrl).setData("fileName", fileName).setData("suffix", ".png");eventProducer.fireEvent(event);Map<String, Object> map = new HashMap<>();map.put("shareUrl", domain + contextPath + "/share/image" + fileName);return CommunityUtil.getJSONString(0, null, map);}//@GetMapping(path = "/share/image/{fileName}")public void getShareImage(@PathVariable("fileName") String fileName, HttpServletResponse response){if(StringUtils.isBlank(fileName)){throw new IllegalStateException("the file name cannot be blank");}response.setContentType("image/png");File file = new File(wkImageStorage + "/" + fileName + ".png");try {OutputStream os = response.getOutputStream();FileInputStream fis = new FileInputStream(file);byte[] data = new byte[1024];int len = 0;while ((len = fis.read(data)) != -1) {os.write(data, 0, len);}} catch (IOException e) {logger.error("querty the long image failed: ", e.getMessage());}}
}
Kafka的消费者,定义如何消费生成长图的事件:
@Component
public class EventConsumer implements CommunityConstant {@Autowiredprivate static final Logger logger = LoggerFactory.getLogger(EventConsumer.class);@Value("${wk.image.storage}")private String wkImageStorage;@Value("${wk.image.command}")private String wkImageCommand;@KafkaListener(topics = {TOPIC_SHARE})public void handleShareMessage(ConsumerRecord record) {if (record == null || record.value() == null) {logger.error("the content of the message is empty");return;}Event event = JSONObject.parseObject(record.value().toString(), Event.class);if (event == null) {logger.error("message format error");return;}String htmlUrl = (String) event.getData().get("htmlUrl");String fileName = (String) event.getData().get("fileName");String suffix = (String) event.getData().get("suffix");String cmd = wkImageCommand + " --quality 75 " + htmlUrl + " " + wkImageStorage + "/" + fileName + suffix;try {Runtime.getRuntime().exec(cmd);logger.info("generate long image successfully: " + cmd);} catch (IOException e) {logger.info("generate long image fail: " + e.getMessage());}}
}
测试
相关文章:

使用wkhtmltoimage实现生成长图分享
需求 用户可以选择以长图的形式分享本网页 方法 wkhtmltopdf wkhtmltopdf url filewkhtmltoimage url file java Runtime.getRuntime().exec() 下载 直接去官网下载对应的版本:官网 命令行使用WK > wkhtmltopdf https://www.nowcoder.com /opt/project/…...

新风机未来什么样?
新风机在未来将会有许多令人期待的发展和改进,让我们一起来看一看吧!以下是新风机未来的一些可能性: 智能化和智能家居:新风机将更多地与智能家居系统整合,通过物联网和人工智能技术,实现智能控制和智能调节…...

python的几种数据类型的花样玩法(一)
Python是一种动态类型语言,这意味着在Python中,你可以操作多种不同的数据类型,而且这些数据类型可以自动转换。以下是一些Python数据类型的花样玩法: 数字: 整数 (1, 100, -786 等) 浮点数 (1.1, -78.4, 3.14159 等)…...

python回调函数之获取jenkins构建结果
前言 嗨喽~大家好呀,这里是魔王呐 ❤ ~! python更多源码/资料/解答/教程等 点击此处跳转文末名片免费获取 需求背景: 现在用jenkins构建自动化测试(2个job),公司现将自动化纳入到发布系统 要求每次构建成功之后&am…...

Docker底层实现
Docker采用c/s架构,Docker守护进程( Daemon )作为服务端,接受来自客户端(命令行)的请求,并处理这些请求(创建、运行、分发容器) 。客户端和服务端既可以运行在一个机器上…...

PY32F003F18之RS485通讯
PY32F003F18将USART2连接到RS485芯片,和其它RS485设备实现串口接收后再转发的功能。 一、测试电路 二、测试程序 #include "USART2.h" #include "stdio.h" //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf() #include "…...

概率论与数理统计学习笔记(7)——全概率公式与贝叶斯公式
目录 1. 背景2. 全概率公式3. 贝叶斯公式 1. 背景 下图是本文的背景内容,小B休闲时间有80%的概率玩手机游戏,有20%的概率玩电脑游戏。这两个游戏都有抽卡环节,其中手游抽到金卡的概率为5%,端游抽到金卡的概率为15%。已知小B这天抽…...

深入了解 Axios 的 put 请求:使用技巧与最佳实践
在前端开发中,我们经常需要与后端服务器进行数据交互。其中,PUT 请求是一种常用的方法,用于向服务器发送更新或修改数据的请求。通过发送 PUT 请求,我们可以更新服务器上的资源状态。 Axios 是一个流行的 JavaScript 库࿰…...

浅谈Http协议、TCP协议(转载)
TCP标志位,有6种标示:SYN(synchronous建立联机) ,ACK(acknowledgement 确认) ,PSH(push传送),FIN(finish结束) ,RST(reset重置), URG(urgent紧急) Sequence number(顺序号码) ,Acknowledge num…...

flatten-maven-plugin使用
这篇文章主要介绍了flatten-maven-plugin使用,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 − 目录 一、简介 1.1 作用1.2 goal介绍二、使用总结 一、简介 1.1 作用 将pom工程父子pom的版…...

Vue3中快速简单使用CKEditor 5富文本编辑器
Vue3简单使用CKEditor 5 前言准备定制基础配置富文本配置目录当前文章demo目录结构 快速使用demo 前言 CKEditor 5就是内嵌在网页中的一个富文本编辑器工具 CKEditor 5开发文档(英文):https://ckeditor.com/docs/ckeditor5/latest/index.htm…...

qt简易网络聊天室 数据库的练习
qt网络聊天室 服务器: 配置文件.pro QT core gui networkgreaterThan(QT_MAJOR_VERSION, 4): QT widgetsCONFIG c11# The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exac…...

Navicat连接mysql8.0:提示无法加载身份验证插件“caching_sha2_password”
Navicat连接mysql时,提示:Unable to load authentication plugin ‘caching_sha2_password‘. 原因:mysql 8.0 默认使用 caching_sha2_password 身份验证机制。 D:\MySQL8.0\install\bin>mysql -uroot -p123456789 #登录 mysql: [War…...

手写签名到背景上合为1张图
手写签名到背景上合为1张图 package.json中 "signature_pad": "3.0.0-beta.3"<template><div class"home"><canvas id"canvas" width"500" height"300"></canvas><button click"…...

华为认证系统学习大纲及课程
前言 任何学习过程都需要一个科学合理的学习路线,才能够有条不紊的完成我们的学习目标。华为认证网络工程师所需学习的内容纷繁复杂,难度较大,所以今天特别为大家整理了一个全面的华为认证网络工程师学习大纲及课程,帮大家理清思…...

某米ax3000路由器组网解析
我们使用某米k60手机与某米ax3000 wifi6路由器组网,来分析和学习网络速率与瓶颈限制。 某米 AX3000 路由器简介 某米 AX3000 路由器是一款支持 WiFi 6 的双频路由器,它的 MIMO 是 22,也就是两根天线。MIMO 是 Multiple Input Multiple Outpu…...

【leetcode 力扣刷题】数学题之除法:哈希表解决商的循环节➕快速乘求解商
两道和除法相关的力扣题目 166. 分数到小数29. 两数相除快速乘解法一:快速乘变种解法二: 二分查找 快速乘 166. 分数到小数 题目链接:166. 分数到小数 题目内容: 题目是要我们把一个分数变成一个小数,并以字符串的形…...

Union类型和集合的union()方法-set.union()
Union类型和集合的Union 方法 一、Union类型1.Union类型由来2.Union类型的语法3.Union类型的使用4.一些等价写法 二、Set.union()union() 语法示例代码 一、Union类型 1.Union类型由来 Python中的Union类型是 3.10版本引入的新功能之一。它是一种特殊的类型注释,用…...

简明SQL别名指南:掌握AS实现列名更名
在 SQL 查询中,使用 {原始字段名} as {别名} 的语法来为查询结果的列赋予更直观的名称,以提高查询结果的可读性和可理解性。 以下是用到的表。 用AS更名 例如,查询表1的name字段,并将其更名为"名字",同时查…...

基于量子密钥分发和区块链技术的新一代加密通信系统
量子通信与区块链构建下一代加密通信基础设施 量子技术和区块链技术是国家信息安全和国家数字化转型的重要组成部分,在国家战略中具有重要地位。“十四五”规划纲要将“加快数字发展建设数字中国”作为独立篇章,指出要进一步明确发展云计算、大数据、物联…...

网络安全-子域名收集
本文为作者学习文章,按作者习惯写成,如有错误或需要追加内容请留言(不喜勿喷) 本文为追加文章,后期慢慢追加 子域名 子域名指二级域名,二级域名是顶级域名(一级域名)的下一级比如mail.heetian.com和bbs.heetian.com…...

go-zero jwt 鉴权快速实战
前面我们分享了 go-zero 的快速实战以及日志组件的剖析,本次我们来实战使用 go-zero jwt 鉴权 本次文章主要是分享关于 go-zero 中 jwt 的使用方式,会以一个 demo 的方式来进行实战,对于使用 goctl 工具以及安装细节就不在赘述,有…...

9.8day58 单调栈
739. 每日温度 - 力扣(LeetCode) 知识点:1.建栈 2.如果后面要加入的数小于栈顶元素就把数组的下标压进栈里 3.反之 就让该数于栈顶元素进行比较 如果该数大于栈顶元素(while) 就把栈顶元素下表对应的arr数组的值进行…...

快速完成工信部APP备案流程_以阿里云APP备案为例
阿里云APP备案流程分为6步,APP备案成功后应用可以上架,登录阿里云账号填写APP信息,等待阿里云初审,初审通过后进行工信部短信核验,管局审核通过后APP即可备案成功,最后移动APP应用可以分发平台上架…...

uniapp中UView中 u-form表单在v-for循环下如何进行表单校验
1、数据data格式 注:rule绑定的tableFromRule中要和表单tableFrom下面放置一个同名数组,确保u-form能找到 tableFrom: {tableData: [//数据详情列表]},tableFromRule: {//校验tableData: [//数据详情列表]},formRules:{localation:[{required: true,mes…...

工作新时代,腾讯轻联塑造高效办公未来
腾讯轻联:开启便捷、高效的集成新纪元 ⭐ 写在前面⭐ 使用模板快速起步⭐ 自定义流程初体验⭐ 无与伦比的集成强者⭐ 写在最后 ⭐ 写在前面 在当今竞争激烈的商业环境中,提高企业的办公效率和工作流程自动化变得至关重要。腾讯轻联,作为新一…...

JavaScript实现广告倒计时和跳过广告
倒计时和跳过广告 最近打开手机上的app,映入眼帘的都是一个几秒的广告,带有倒计时,当然如果不喜欢的话可以点击跳过,跳过广告其实质应该就是关闭广告。以前用JavaScript做过一个定时关闭的广告,于是把代码完善了一下&…...

蚂蚁发布金融大模型:两大应用产品支小宝2.0、支小助将在完成备案后
9月8日,在上海举办的外滩大会上,蚂蚁集团正式发布金融大模型。据了解,蚂蚁金融大 模型基于蚂蚁自研基础大模型,针对金融产业深度定制,底层算力集群达到万卡规模。该大 模型聚焦真实的金融场景需求,在“认知…...

Jenkins 持续集成:Linux 系统 两台机器互相免密登录
背景知识 我们把public key放在远程系统合适的位置,然后从本地开始进行ssh连接。 此时,远程的sshd会产生一个随机数并用我们产生的public key进行加密后发给本地,本地会用private key进行解密并把这个随机数发回给远程系统。 最后…...

Golang-GJSON 快速而简单的方法来从 json 文档获取值
GJSON 是一个 Go 包,它提供了一种快速而简单的方法来从 json 文档获取值。它具有单行搜索、点符号路径、迭代和解析 json 行等功能。 GJSON 也可用于Python和Rust 入门 安装中 要开始使用GJSON 请安装 Go 并运行 go get : $ go get -u github.com/ti…...