Angular NG2-PDF浏览器修改SRC
我正在将NG2-PDF观看器用于模态组件来查看PDF。 PDF是用url.createobjecturl
指令创建的。 在Init上,我签署了PDF。当我将新值设置为SRC变量时,直到我单击PDF之前,它才显示。
@Component({
selector: 'app-trip-detail-pdf',
template: `
<div class="form-group">
<div class="row">
<div class="col-12">
<pdf-viewer #pdfViewer
[src]="documentData"
[render-text]="true"
[original-size]="false"
[autoresize]="false"
[show-all]="true"
[fit-to-page]="false"
[zoom]="1"
[zoom-scale]="'page-width'"
style="width: 100%;"
[style.height.vh]="loadingPlanDetail?.IsSigned? 90: (bs.xs$|async)?70:(bs.sm$|async)&&(bs.handSetLandscape$|async)?40:(bs.tablet$|async)?60:72"
></pdf-viewer>
</div>
<div class="pt-1 col-12" >
<app-signature (signed)="signedPdf($event)" *ngIf="!loadingPlanDetail?.IsSigned"></app-signature>
</div>
</div>
</div>
`,
styles: [
]
})
export class DeliverDetailPdfComponent implements OnInit {
@ViewChild('pdfViewer', { static: true }) pdfViewer: PdfViewerComponent;
@Input() documentData: any;
@Input() loadingPlanDetail: LoadingPlanDetailModel;
@Input() deliverHeader: DeliverHeaderModel;
@Output() td: EventEmitter<any> = new EventEmitter();
pdfArray: any;
pdfDoc: PDFDocument;
constructor(public bs: BreakpointService, private store: Store) { }
ngOnInit() {
this.drawDriverSign()
}
async drawDriverSign() {
console.log("drawDriverSign")
this.pdfArray = await this.loadingPlanDetail.Pdf.arrayBuffer();
this.pdfDoc = await PDFDocument.load(this.pdfArray)
const signDriverImage = await this.pdfDoc.embedPng(this.deliverHeader.Sign);
const signImageSize = signDriverImage.size();//.scale(0.25)
//get first page dimension
const pages = this.pdfDoc.getPages()
const firstPage = pages[0]
const { width, height } = firstPage.getSize()
const page = this.pdfDoc.addPage([width, height]);
//inizio - firma vettore
page.drawRectangle({
x: 20,
y: 700,
width: 350,
height: 100,
borderColor: rgb(0, 0, 1),
rotate: degrees(0),
xSkew: degrees(0),
ySkew: degrees(0),
})
page.drawText('Firma vettore', {
x: 25, y: 802, size: 10, color: rgb(0, 0, 0)
})
page.drawImage(signDriverImage, {
x: 25, //page.getWidth() / 2 - signImageSize.width / 2,
y: 680, //page.getHeight() / 2 - signImageSize.height / 2,
width: signImageSize.width,
height: signImageSize.height,
});
//fine - firma vettore
const pdfSave = await this.pdfDoc.save()
const pdfBlob = new Blob([pdfSave], { type: 'application/pdf' });
this.documentData = URL.createObjectURL(pdfBlob);
}
}
I'm using ng2-pdf-viewer into a modal component to see a PDF. The PDF is created from a blob with URL.createObjectURL
instruction.
On init, I signed the PDF. When I set new value to src variable, it is not displayed until I click on the PDF.
@Component({
selector: 'app-trip-detail-pdf',
template: `
<div class="form-group">
<div class="row">
<div class="col-12">
<pdf-viewer #pdfViewer
[src]="documentData"
[render-text]="true"
[original-size]="false"
[autoresize]="false"
[show-all]="true"
[fit-to-page]="false"
[zoom]="1"
[zoom-scale]="'page-width'"
style="width: 100%;"
[style.height.vh]="loadingPlanDetail?.IsSigned? 90: (bs.xs$|async)?70:(bs.sm$|async)&&(bs.handSetLandscape$|async)?40:(bs.tablet$|async)?60:72"
></pdf-viewer>
</div>
<div class="pt-1 col-12" >
<app-signature (signed)="signedPdf($event)" *ngIf="!loadingPlanDetail?.IsSigned"></app-signature>
</div>
</div>
</div>
`,
styles: [
]
})
export class DeliverDetailPdfComponent implements OnInit {
@ViewChild('pdfViewer', { static: true }) pdfViewer: PdfViewerComponent;
@Input() documentData: any;
@Input() loadingPlanDetail: LoadingPlanDetailModel;
@Input() deliverHeader: DeliverHeaderModel;
@Output() td: EventEmitter<any> = new EventEmitter();
pdfArray: any;
pdfDoc: PDFDocument;
constructor(public bs: BreakpointService, private store: Store) { }
ngOnInit() {
this.drawDriverSign()
}
async drawDriverSign() {
console.log("drawDriverSign")
this.pdfArray = await this.loadingPlanDetail.Pdf.arrayBuffer();
this.pdfDoc = await PDFDocument.load(this.pdfArray)
const signDriverImage = await this.pdfDoc.embedPng(this.deliverHeader.Sign);
const signImageSize = signDriverImage.size();//.scale(0.25)
//get first page dimension
const pages = this.pdfDoc.getPages()
const firstPage = pages[0]
const { width, height } = firstPage.getSize()
const page = this.pdfDoc.addPage([width, height]);
//inizio - firma vettore
page.drawRectangle({
x: 20,
y: 700,
width: 350,
height: 100,
borderColor: rgb(0, 0, 1),
rotate: degrees(0),
xSkew: degrees(0),
ySkew: degrees(0),
})
page.drawText('Firma vettore', {
x: 25, y: 802, size: 10, color: rgb(0, 0, 0)
})
page.drawImage(signDriverImage, {
x: 25, //page.getWidth() / 2 - signImageSize.width / 2,
y: 680, //page.getHeight() / 2 - signImageSize.height / 2,
width: signImageSize.width,
height: signImageSize.height,
});
//fine - firma vettore
const pdfSave = await this.pdfDoc.save()
const pdfBlob = new Blob([pdfSave], { type: 'application/pdf' });
this.documentData = URL.createObjectURL(pdfBlob);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,将DocumentData设置为null,然后将其重新分配新的Blob URL。
this.documentData = null;
this.documentData = url.CreateObjectUrl(pdfblob);
First you set the documentData as a null,and then reassign it the new blob URL.
this.documentData = null;
this.documentData = URL.createObjectURL(pdfBlob);