我正在尝试使用 PDFJS 从 PDF 中提取元数据并通过 Observable 订阅获取它。
服务代码:
export interface PdfProperties {
title: string;
author: string;
subject: string;
keywords: string;
}
...
extractPdfProperties(file: Uint8Array): any {
const pdfPropertiesObservable = new Observable<PdfProperties>( (observer) => {
const pdfLoading = pdfjsLib.getDocument(file);
pdfLoading.promise.then( pdf => {
pdf.getMetadata().then( metadata => {
this.pdfProperties.title = metadata.info.Title;
this.pdfProperties.author = metadata.info.Author;
this.pdfProperties.keywords = metadata.info.Keywords;
this.pdfProperties.subject = metadata.info.Subject;
observer.next(this.pdfProperties);
return this.pdfProperties;
});
});
});
return pdfPropertiesObservable;
}
组件代码
onFileSelect(input: HTMLInputElement) {
const fileReader = new FileReader();
fileReader.readAsArrayBuffer(input.files[0]);
fileReader.onload = () => {
const typedArray = new Uint8Array(fileReader.result as Uint8Array);
const pdfObservable$ = this.pdfProperties.extractPdfProperties(typedArray);
pdfObservable$.subscribe((subscriptionpdfp: PdfProperties) => { this.pdfp = subscriptionpdfp; });
};
}
没有错误,但 this.pdfp 一直未定义。
我究竟做错了什么 ?
在此先感谢您的帮助!