ionic-framework - 将 dom 转换为图像时隐藏 div
问题描述
我目前正在使用domtoimage库打印(ios 设备)并在桌面上下载为 PDF
HTML:
<div class="row button__center" *ngIf="btnDownloaderVisible">
<div>
<ion-row>
<ion-col size="6" class="ion-text-right">
<div class="form-group">
<button class="btn btn-block btn" (click)="printScenario()" *ngIf="platformModeDevice">
<ion-icon name="download-outline"></ion-icon> Print </button>
<button class="btn btn-block btn" (click)="printScenario()" *ngIf="!platformModeDevice">
<div class="icon">
<i class="mdi mdi-download"></i>
</div
> Download </button>
</div>
</ion-col>
<ion-col size="6" class="ion-text-left">
<div class="form-group">
<button class="btn btn-block btn" (click)="sendMailScenario()">
<ion-icon name="mail-outline"></ion-icon>
Send</button>
</div>
</ion-col>
</ion-row>
</div>
</div>
TS:
async printScenario() {
let loading = await this.loadingController.create({
message:'Generating...',
animated:true,
spinner:'dots',
mode:'ios'
});
try {
this.btnDownloaderVisible = false;
console.log(this.btnDownloaderVisible);
loading.present();
var node = document.getElementById('pdfContainer');
var img;
var currentdate = new Date();
var filename = this.currentScenario.companyId.name + "-" +currentdate.getDate() + "-" + (currentdate.getMonth()+1) + "-" + currentdate.getFullYear() ;
var newImage;
domtoimage.toPng(node, { bgcolor: '#fff', height:1024 })
.then((dataUrl) => {
img = new Image();
img.src = dataUrl;
newImage = img.src;
if(this.platform.is('ios') || this.platform.is('iphone') || this.platform.is('ipad')) {
let options: PrintOptions = {
name: 'Simulator',
duplex: true,
orientation: 'landscape',
monochrome:false
}
newImage = newImage.replace('data:image/png;base64,','base64://');
this.printer.print(newImage, options).then(()=> {
}, (err:any)=> {
});
} else {
img.onload = function () {
var pdfWidth = img.width;
var pdfHeight = img.height;
var doc;
if(pdfWidth > pdfHeight)
{
doc = new JSPDF('l', 'px', [pdfWidth , pdfHeight]);
}
else
{
doc = new JSPDF('p', 'px', [pdfWidth , pdfHeight]);
}
var width = doc.internal.pageSize.getWidth();
var height = doc.internal.pageSize.getHeight();
doc.addImage(newImage, 'PNG', 10, 10, width, height);
doc.save(filename);
loading.dismiss();
}
}
})
.catch((error) =>{}).finally(()=> {
loading.dismiss();
this.btnDownloaderVisible = true;
console.log(this.btnDownloaderVisible)
});
} catch(e) {
loading.dismiss();
this.utils.showAlert('Unable to process request, please try again');
}
}
正如您在 HTML 的第一个 div 中看到的那样*ngIf="btnDownloaderVisible"
所以我想在打印函数之前隐藏这个 div,所以在函数中,我 this.btnDownloaderVisible = false;
在try
. 因此,当我运行该功能时,它会正确地从 HTML 中隐藏按钮,但在结果(打印视图)或 pdf 文件中,它不会隐藏它。我究竟做错了什么?
解决方案
推荐阅读
- mysql - 无法通过箭头将 SQL 转储文件中的输入传递给 docker 中的 mysql 命令
- javascript - 正则表达式:允许在 excel 文件名中使用逗号
- javascript - 如何检查是否从api返回数据?
- python - 使用 Django 中当前生成的 pdf 发送 Gmail
- c++ - 如何读取文件中特定数量的行
- c++ - 在什么情况下我们会在 C++ 中得到 nan?
- ibm-midrange - Zend PHP 代码是否必须更改才能在社区 PHP 上运行?
- scala - 跨不同 TableQuery[T] 实现通用功能
- python - 我如何询问用户是否想再次使用我的搜索脚本来搜索其他内容?
- mongodb - 如何在 Kotlin 中将 POJO 与 Quarkus 和 MongoDB 一起使用