给在线盖章图绘制成图片保存本地

最近做一个微信小程序开电子发票并下载保存本地一个功能

当盖章图是本地图片,绘制发票保存是可以连同图片一起绘制保存本地的

但是当图片是接口拿的一个https图片在线地址,渲染在页面没问题,但是一旦绘制发票保存本地,就会发现发票上的这个在线地址盖章图片没有绘制成功

因为在线地址需要先使用

uni.downloadFile

把图片下载下来然后进行绘制才能保存下来

javascript 复制代码
	// 绘制合计行
					x = margin;
					const totalData = ['合计',
						`应付:${this.detailInfo.totalActualReceivable}.优惠:${this.detailInfo.totalDiscountAmount}.收款${this.detailInfo.totalReceivedAmount}${this.convertToChinese(this.detailInfo.totalReceivedAmount)}`
					];
					for (let i = 0; i < totalData.length; i++) {
						ctx.fillText(totalData[i], x, y);
						x += 450;
					}
					y += lineHeight;

					// 绘制分割线
					ctx.beginPath();
					ctx.moveTo(margin, y);
					ctx.lineTo(800, y);
					ctx.stroke();
					y += lineHeight;

					// 绘制底部说明
					ctx.setFontSize(14);
					ctx.fillText('*凭本收据可换取正式发票,盖章有效,请妥善保存,遗失不补。', margin, y);
					y += lineHeight;

					// 绘制备注和收款人
					ctx.fillText('备注:-', margin, y);
					ctx.fillText(`收款人:${this.detailInfo.cashier}`, 600, y);
					y += lineHeight * 2;
					if (this.gzimg) {
						// 下载网络图片到本地
						const downloadRes = await new Promise((resolve, reject) => {
							uni.downloadFile({
								url: this.gzimg,
								success: (res) => {
									if (res.statusCode === 200) {
										resolve(res.tempFilePath);
									} else {
										reject(new Error('下载失败'));
									}
								},
								fail: reject
							});
						});

						// 使用本地临时路径绘制
						const stampX = 550;
						const stampY = y - 200;
						const stampSize = 100;
						ctx.drawImage(downloadRes, stampX, stampY, stampSize, stampSize);
					}
					// 绘制完成
					ctx.draw(false, () => {
						uni.canvasToTempFilePath({
							canvasId: 'myCanvas',
							success: (res) => {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: () => {
										uni.showToast({
											title: '保存成功',
											icon: 'success'
										});
									},
									fail: (err) => {
										uni.showToast({
											title: '保存失败',
											icon: 'none'
										});
										console.error(err);
									}
								});
							},
							fail: (err) => {
								console.error('canvas转图片失败', err);
							}
						});
					});

记录一下~

相关推荐
꯭爿꯭巎꯭2 天前
Notepad++下载 Notepad下载
notepad++
Ai老司机5 天前
Notepad++ 中文版下载安装完整指南(2026最新,8.9.3版)
文本编辑器·notepad++·代码编辑器·notepad++下载·notepad++安装教程·notepad++中文版·免费编辑器
px不是xp5 天前
微信小程序组件化开发最佳实践
微信小程序·小程序·notepad++
普通人65 天前
微信小程序又双叒叕改获取头像昵称的接口了
微信小程序·小程序·notepad++
xiaoliuliu123458 天前
Notepad++ 8.6 文本编辑器安装教程:详细步骤+自定义路径+桌面快捷方式
notepad++
多年小白9 天前
2026年AI智能体“三国杀“:腾讯龙虾矩阵、阿里千问生态与字节豆包的技术架构全解析
网络·人工智能·科技·矩阵·notepad++
Ai老司机10 天前
Notepad++ 最新版下载
notepad++·notepad·notepad 官网下载地址
路边闲人210 天前
Notepad++插件安装,连不上网怎么办?
notepad++
人还是要有梦想的15 天前
如何开发微信小程序
微信小程序·小程序·notepad++
叶子野格16 天前
Notepad++编写html文件使用D3绘图:数据可视化
笔记·学习·信息可视化·开源·notepad++