我正在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
我尝试实现答案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>
答案 0 :(得分:1)
简答:
<pdf-viewer [src]="safeUrl.changingThisBreaksApplicationSecurity"></pdf-viewer>
你可以通过 .changeThisBreaksApplicationSecurity 访问 url, 希望这对您或其他遇到相同问题的人有所帮助。
如果有其他要求,请回复此帖子。
答案 1 :(得分:0)
如果您使用的是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;
}
}