[英]How to Show PDF file from local in Angular?
I have angular web application and I need to show and print PDF which located in my computer.我有 angular web 应用程序,我需要显示并打印位于我的计算机中的 PDF。 How to show PDF file with print options?
如何显示带有打印选项的 PDF 文件? I want to show pdf located like C:\Users\xx\Desktop\xyz.pdf.
我想显示 pdf 的位置类似于C:\Users\xx\Desktop\xyz.pdf。
<iframe
src="https://drive.google.com/viewerng/viewer?embedded=true&url=http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&scrollbar=0"
frameBorder="0"
scrolling="auto"
height="100%"
width="100%"
></iframe>
I have tried iframe and other tools, but as I have to show from my local, it gives an error.我已经尝试过 iframe 和其他工具,但正如我必须从本地显示的那样,它给出了一个错误。
You can use ng2-pdf-viewer package.您可以使用ng2-pdf-viewer package。
Steps:脚步:
Import PdfViewerModule to your module:将 PdfViewerModule导入您的模块:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { PdfViewerModule } from 'ng2-pdf-viewer';
@NgModule({
imports: [BrowserModule, PdfViewerModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
class AppModule {}
After use this component in your tempalte:在您的模板中使用此组件后:
import { Component } from '@angular/core';
@Component({
selector: 'example-app',
template: `
<pdf-viewer [src]="pdfSrc"
[render-text]="true"
style="display: block;"
></pdf-viewer>
`
})
export class AppComponent { }
Render local PDF file渲染本地 PDF 文件
In your html template add input:在您的 html 模板中添加输入:
<input (change)="onFileSelected()" type="file" id="file">
and then add onFileSelected method to your component:然后将 onFileSelected 方法添加到您的组件中:
onFileSelected() {
let $img: any = document.querySelector('#file');
if (typeof (FileReader) !== 'undefined') {
let reader = new FileReader();
reader.onload = (e: any) => {
this.pdfSrc = e.target.result;
};
reader.readAsArrayBuffer($img.files[0]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.