Angular PDFJS元数据提取的角度可观测性
我正在尝试使用PDFJS从PDF中提取元数据,并通过可观察订阅获取元数据 服务代码:Angular PDFJS元数据提取的角度可观测性,angular,pdfjs-dist,Angular,Pdfjs Dist,我正在尝试使用PDFJS从PDF中提取元数据,并通过可观察订阅获取元数据 服务代码: export interface PdfProperties { title: string; author: string; subject: string; keywords: string; } ... extractPdfProperties(file: Uint8Array): any { const pdfPropertiesObservable = new Obse
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;
}
没有错误,但this.pdfp一直未定义
我做错了什么
提前感谢您的帮助 您可以使用RxJS函数将承诺转换为可观测值,而不是手动创建可观测值。从那里,您可以应用任何RxJS操作符将响应通知转换为所需的形式 我使用操作符从一个可观察对象映射到另一个可观察对象,并使用操作符将通知转换为实例类型
PdfProperties
的对象
试试下面的方法
导出接口PDFProperty{
标题:字符串;
作者:字符串;
主题:字符串;
关键词:字符串;
}
extractPdfProperties(文件:Uint8Array):可观察{//{
this.pdfp=subscriptionpdfp;
});
};
}
您可以使用RxJS函数将承诺转换为可观察,而不是手动创建可观察。从那里,您可以应用任何RxJS操作符将响应通知转换为所需的形式
我使用操作符从一个可观察对象映射到另一个可观察对象,并使用操作符将通知转换为实例类型PdfProperties
的对象
试试下面的方法
导出接口PDFProperty{
标题:字符串;
作者:字符串;
主题:字符串;
关键词:字符串;
}
extractPdfProperties(文件:Uint8Array):可观察{//{
this.pdfp=subscriptionpdfp;
});
};
}
谢谢。看起来应该指定映射中元数据的类型,否则它会抱怨不知道“info”位。(类型“未知”上不存在属性“info”)。你知道那是什么类型的吗?那很可能是TS Lint错误。尽管如此,该代码仍应运行良好。它显示该错误是因为Linter不知道来自getMetadata()
调用的响应的属性。您可以使用括号符号而不是点符号来访问属性,从而快速修复它。我已经更新了答案。另一种方法是从getMetadata()
函数定义响应签名的接口,并定义metadata
变量的类型。谢谢。看起来应该指定映射中元数据的类型,否则它会抱怨不知道“info”位。(类型“未知”上不存在属性“info”)。你知道那是什么类型的吗?那很可能是TS Lint错误。尽管如此,该代码仍应运行良好。它显示该错误是因为Linter不知道来自getMetadata()
调用的响应的属性。您可以使用括号符号而不是点符号来访问属性,从而快速修复它。我已经更新了答案。另一种方法是从getMetadata()
函数定义响应签名的接口,并定义metadata
变量的类型。
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; });
};
}