我正在尝试在我的Angular 7项目中使用ng2-pdf-viewer。最初,我有一个cors问题,我解决了with this advice。现在,我遇到了以下错误:
Invalid parameter object: need either .data, .range or .url
at Object.getDocument (pdf.js:7919)
at PdfViewerComponent.push.../../../node_modules/ng2-pdf-viewer/ng2-pdf-viewer.es5.js.PdfViewerComponent.loadPDF我尝试实现answer from this post,但没有成功。这可能是正确的,但我误解了如何实现。
我的ts:
import {Component, OnInit} from '@angular/core'
import {DomSanitizer, SafeUrl} from '@angular/platform-browser'
@Component({
selector:'app-help-dialog',
templateUrl:'./helpDialog.html',
styleUrls:['../../style/style.css']
})
export class HelpDialog implements OnInit {
pdfSource:string = 'https://url/to/my/pdf.pdf'
safeUrl:SafeUrl
constructor(private sanitizer: DomSanitizer) {
}
ngOnInit() {
this.safeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.pdfSource)
}
}
和我的模板:
<pdf-viewer [src]='safeUrl'>
</pdf-viewer>发布于 2019-06-05 21:09:29
如果你使用的是url,那么你应该做的就是用pdfSource代替safeUrl。
如果你使用的是base64编码,这是我修复它的方法:在.html文件中
<pdf-viewer [src]="pdfSource"></pdf-viewer>在.ts文件中
import { Component, OnInit } from '@angular/core';
@Component({
...
})
export class testComponent implements OnInit {
base_64_string = 'this_is_where_you_put_base_64';
pdfSource;
constructor() {}
ngOnInit() {
this.pdfSource = this._base64ToArrayBuffer(this.pdfSource);
}
_base64ToArrayBuffer(base64) {
const binary_string = window.atob(base64);
const len = binary_string.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
}发布于 2021-02-19 14:00:23
简短的回答:
<pdf-viewer [src]="safeUrl.changingThisBreaksApplicationSecurity"></pdf-viewer>
你可以通过.changingThisBreaksApplicationSecurity访问url,希望这对你或其他有同样问题的人有所帮助。
如果有其他要求,请回复此帖子。
https://stackoverflow.com/questions/53877582
复制相似问题