How to Open a PDF or an Image in Angular Without Dependencies
Step-by-Step Guide
<div *ngIf="fileUrl != null && (this.isPdf || this.isImage)" class="fixed inset-0 bg-gray-800 bg-opacity-50 flex z-[60] justify-center items-center backdrop-blur-md">
<div class="bg-white w-[90%] h-[90%] rounded-lg flex flex-col">
<div class="flex justify-between items-center p-4 border-b">
<h2 class="text-lg font-semibold">Document Viewer</h2>
<button class="text-gray-500 hover:text-gray-700" (click)="closePdf()">×</button>
</div>
<div *ngIf="isPdf; else imageTemplate">
<!-- PDF Rendering -->
<object [attr.data]="fileUrl" type="application/pdf" width="100%" height="800">
<p>Your browser does not support PDFs. <a [href]="fileUrl" download="document.pdf">Download PDF</a></p>
</object>
</div>
<!-- Image Rendering -->
<ng-template #imageTemplate>
<img *ngIf="isImage" [src]="fileUrl" alt="Preview" style="max-width: 100%; max-height: 800px;"/>
</ng-template>
</div>
</div>5. Additional Considerations
Conclusion
Last updated