نمایش، ذخیره و چاپ فایلهای PDF در برنامههای Angular
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۱۱/۰۲ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using Microsoft.AspNetCore.Mvc;
namespace AngularTemplateDrivenFormsLab.Controllers
{
[Route("api/[controller]")]
public class ReportsController : Controller
{
[HttpGet("[action]")]
public IActionResult GetPdfReport()
{
return File(virtualPath: "~/assets/sample.pdf",
contentType: "application/pdf",
fileDownloadName: "sample.pdf");
}
}
} import { Injectable } from "@angular/core";
import { Observable } from "rxjs/Observable";
import { HttpClient } from "@angular/common/http";
@Injectable()
export class DownloadPdfDataService {
constructor(private httpClient: HttpClient) { }
public getReport(): Observable<Blob> {
return this.httpClient.get("/api/Reports/GetPdfReport", { responseType: "blob" });
}
} img-src 'self' data: blob: default-src 'self' blob: object-src 'self' blob:
import { DownloadPdfDataService } from "./../download-pdf-data.service";
import { WindowRefService } from "./../../core/window.service";
import { Component, OnInit } from "@angular/core";
import { DomSanitizer, SafeResourceUrl } from "@angular/platform-browser";
@Component({
templateUrl: "./view-pdf.component.html",
styleUrls: ["./view-pdf.component.css"]
})
export class ViewPdfComponent implements OnInit {
private nativeWindow: Window;
private pdfBlobUrl: string;
sanitizedPdfBlobResourceUrl: SafeResourceUrl;
constructor(private downloadService: DownloadPdfDataService,
private windowRefService: WindowRefService, private sanitizer: DomSanitizer) { }
ngOnInit() {
this.nativeWindow = this.windowRefService.nativeWindow;
this.downloadService.getReport().subscribe(pdfDataBlob => {
console.log("pdfDataBlob", pdfDataBlob);
const urlCreator = this.nativeWindow.URL;
this.pdfBlobUrl = urlCreator.createObjectURL(pdfDataBlob);
console.log("pdfBlobUrl", this.pdfBlobUrl);
this.sanitizedPdfBlobResourceUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.pdfBlobUrl);
});
}
} <h1>Display PDF Files</h1>
<div *ngIf="sanitizedPdfBlobResourceUrl">
<h4>using iframe</h4>
<iframe width="100%" height="600" [attr.src]="sanitizedPdfBlobResourceUrl" type="application/pdf"></iframe>
<h4>using object</h4>
<object [attr.data]="sanitizedPdfBlobResourceUrl" type="application/pdf" width="100%"
height="100%"></object>
<h4>usin embed</h4>
<embed [attr.src]="sanitizedPdfBlobResourceUrl" type="application/pdf" width="100%"
height="100%">
</div> this.sanitizedPdfBlobResourceUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.pdfBlobUrl);
printPdf() {
const iframe = document.createElement("iframe");
iframe.style.display = "none";
iframe.src = this.pdfBlobUrl;
document.body.appendChild(iframe);
iframe.contentWindow.print();
} showPdf() {
this.nativeWindow.open(this.pdfBlobUrl);
} downloadPdf() {
const fileName = "test.pdf";
const anchor = document.createElement("a");
anchor.style.display = "none";
anchor.href = this.pdfBlobUrl;
anchor.download = fileName;
document.body.appendChild(anchor);
anchor.click();
} <a href="computer.png" download="PC.png"> Download File </a>