简体   繁体   English

如何从 Angular 应用程序打开 PDF 文件

[英]How to open PDF file from Angular application

I am building an Angular app that use a PHP backend.我正在构建一个使用 PHP 后端的 Angular 应用程序。 The process is:过程是:

  • The front call a backend endpoint前端调用后端端点
  • The backend builds a PDF file, store it in a specific folder, and returns the file URL (http://serverhost/tmp/somerandompath/somerandomname.pdf)后端构建一个PDF文件,存放在特定文件夹中,并返回文件URL(http://serverhost/tmp/somerandompath/somerandomname.pdf)
  • The front opens a new tab pointing to the PDF file thanks to its URL由于其 URL,前面会打开一个指向 PDF 文件的新标签

My problem is that the PDF url is intercepted by the Angular router and the user is redirected to the homepage (default behavior) instead of the PDF file.我的问题是 PDF url 被 Angular 路由器拦截,用户被重定向到主页(默认行为)而不是 PDF 文件。

Here is my (simplified) routing configuration:这是我的(简化的)路由配置:

const appRoutes: Routes = [
  { path: 'route1', component: Component1 },
  { path: 'route2', component: Component2 },
  { path: 'home', component: HomepageComponent },
  { path: '**', component: HomepageComponent }
];

Do you have any idea how I can open the PDF file?您知道如何打开 PDF 文件吗?

Thank you for your help, Termos感谢您的帮助,泰尔摩斯

First of all I think your url from serve is not correct plz fix it first then You can use this *** ng2-pdf-viewer *** package to render pdf in client side首先,我认为您的服务网址不正确,请先修复它,然后您可以使用此 *** ng2-pdf-viewer *** 包在客户端呈现 pdf

<pdf-viewer [src]="pdfSrc"
          [render-text]="true"
          style="display: block;"
 ></pdf-viewer>

https://github.com/VadimDez/ng2-pdf-viewer#readme https://github.com/VadimDez/ng2-pdf-viewer#readme

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM