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

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

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

但是当图片是接口拿的一个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);
							}
						});
					});

记录一下~

相关推荐
ZC跨境爬虫4 天前
模块化烹饪小程序开发日记 Day2:全局配置与 tabBar 实现
java·前端·javascript·微信小程序·html·notepad++
idolao8 天前
npp.8.5.Installer文本编辑器安装步骤详解(附Notepad++配置与插件安装教程)
notepad++
px不是xp9 天前
【灶台导航】 RAG系统的容错设计:从向量搜索到关键词降级,一个都不能少
javascript·微信小程序·notepad++·rag
空中海10 天前
微信小程序 - 03 工程实践层与综合 Demo
微信小程序·小程序·notepad++
舟遥遥娓飘飘12 天前
面向零基础初学者,从环境搭建到发布上线,手把手教你开发第一个微信小程序(第3章-认识项目结构)
微信小程序·小程序·notepad++
优睿远行12 天前
微信小程序自定义组件开发实战:从封装到发布的全流程指南
微信小程序·小程序·notepad++
喜欢南方姑娘14 天前
微信小程序热更新-用户打开小程序时检测版本自动更新
微信小程序·小程序·notepad++
yzx99101314 天前
从零开始写一个微信小程序:完整代码实战指南(入门篇)
微信小程序·小程序·notepad++
fengyehongWorld16 天前
Notepad++ NppExec插件的使用
notepad++
fengyehongWorld18 天前
Notepad++ 常用插件
notepad++