
Security News
npm βisβ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
ng-pdf-renderer
Advanced tools
A modern, zero-configuration PDF viewer for Angular applications with enhanced error handling, auto-fit, text selection, and responsive design
A modern, zero-configuration PDF viewer for Angular applications with intelligent auto-fit, text selection, and responsive design.
npm install ng-pdf-renderer
import { Component } from '@angular/core';
import { PdfViewerComponent } from 'ng-pdf-renderer';
@Component({
selector: 'app-pdf-demo',
standalone: true,
imports: [PdfViewerComponent],
template: `
<ng-pdf-viewer [src]=\"pdfUrl\"></ng-pdf-viewer>
`
})
export class PdfDemoComponent {
pdfUrl = '/assets/document.pdf';
}
That's it! The PDF viewer will automatically:
interface PdfOptions {
// Display Options
height?: string; // Default: '500px'
width?: string; // Default: '100%'
// Auto-Fit Behavior
autoFit?: boolean; // Default: true
initialZoom?: number; // Default: 1.0 (100%)
initialPage?: number; // Default: 1
// Text & Interaction
enableTextSelection?: boolean; // Default: true
renderTextLayer?: boolean; // Default: true
renderAnnotationLayer?: boolean; // Default: true
// Controls Visibility
showControls?: boolean; // Default: false (hidden)
showNavigation?: boolean; // Default: true
showZoomControls?: boolean; // Default: true
showRotationControls?: boolean; // Default: true
showDownloadButton?: boolean; // Default: true
showPrintButton?: boolean; // Default: true
showSearchBar?: boolean; // Default: true
showThumbnails?: boolean; // Default: false
showOutline?: boolean; // Default: false
}
import { Component } from '@angular/core';
import { PdfViewerComponent, PdfOptions } from 'ng-pdf-renderer';
@Component({
selector: 'app-custom-pdf',
standalone: true,
imports: [PdfViewerComponent],
template: `
<ng-pdf-viewer
[src]=\"pdfUrl\"
[options]=\"pdfOptions\"
(pageChange)=\"onPageChange($event)\"
(documentLoaded)=\"onDocumentLoaded($event)\">
</ng-pdf-viewer>
`
})
export class CustomPdfComponent {
pdfUrl = '/assets/document.pdf';
pdfOptions: PdfOptions = {
height: '800px',
showControls: true, // Show control bar
initialZoom: 1.2, // 120% zoom
autoFit: false, // Disable auto-fit
showThumbnails: true // Show thumbnail panel
};
onPageChange(page: number) {
console.log('Current page:', page);
}
onDocumentLoaded(document: any) {
console.log('PDF loaded:', document.numPages, 'pages');
}
}
Handle PDF loading and rendering errors with the new errorOccurred
event:
@Component({
template: `
<ng-pdf-viewer
[src]="pdfUrl"
(errorOccurred)="onPdfError($event)">
</ng-pdf-viewer>
`
})
export class MyComponent {
onPdfError(error: PdfError): void {
console.error(`${error.type}: ${error.message}`);
// Handle different error types
if (error.type === 'network') {
this.showRetryOption();
} else if (error.type === 'permission') {
this.requestPassword();
}
}
}
Error Interface:
interface PdfError {
type: 'load' | 'render' | 'network' | 'permission' | 'corrupt' | 'timeout' | 'unknown';
message: string;
pageNumber?: number; // For page-specific errors
timestamp: Date;
}
The autoFit
feature (enabled by default) automatically scales PDFs to fit your container:
β οΈ When autoFit: true
(default), manual zoom controls may not work as expected:
// With auto-fit enabled (default):
pdfOptions: PdfOptions = {
showControls: true, // Shows zoom controls
autoFit: true // But auto-fit overrides manual changes!
};
Option 1: Disable Auto-Fit for Manual Control
pdfOptions: PdfOptions = {
autoFit: false, // Disable auto-fit
initialZoom: 1.0, // Set desired zoom
showControls: true // Manual controls work normally
};
Option 2: Use Auto-Fit Only (Recommended)
pdfOptions: PdfOptions = {
autoFit: true, // Let auto-fit handle everything
showControls: false // Hide manual controls to avoid confusion
// PDF scales automatically - no manual intervention needed
};
/* β
Good - Let the PDF scale naturally */
.pdf-container {
width: 100%;
max-width: 1200px;
margin: 0 auto;
}
/* β Avoid - These can cause rendering issues */
.pdf-container {
overflow: hidden; /* Can cut off content */
height: 400px; /* Fixed height without overflow: auto */
max-width: 300px; /* Too restrictive */
}
@Component({
template: `
<ng-pdf-viewer
[src]=\"pdfUrl\"
(pageChange)=\"onPageChange($event)\"
(documentLoaded)=\"onDocumentLoaded($event)\"
(errorOccurred)=\"onError($event)\">
</ng-pdf-viewer>
`
})
export class AdvancedPdfComponent {
onPageChange(pageNumber: number) {
console.log(`User navigated to page ${pageNumber}`);
}
onDocumentLoaded(document: any) {
console.log(`PDF loaded with ${document.numPages} pages`);
}
onError(error: PdfError) {
console.error('PDF error:', error);
}
}
export class PdfSourcesComponent {
// Local file
localPdf = '/assets/document.pdf';
// External URL
externalPdf = 'https://example.com/document.pdf';
// Base64 data
base64Pdf = 'data:application/pdf;base64,JVBERi0xLjQK...';
// Uint8Array (from file upload)
arrayBuffer: Uint8Array;
onFileSelected(event: any) {
const file = event.target.files[0];
if (file.type === 'application/pdf') {
const reader = new FileReader();
reader.onload = (e) => {
this.arrayBuffer = new Uint8Array(e.target?.result as ArrayBuffer);
};
reader.readAsArrayBuffer(file);
}
}
}
PDF not displaying:
// Check console for errors
// Ensure PDF URL is accessible
// Verify CORS headers for external PDFs
Text selection not working:
pdfOptions: PdfOptions = {
enableTextSelection: true, // Ensure this is true
renderTextLayer: true // Required for text selection
};
Manual zoom controls not responding:
pdfOptions: PdfOptions = {
autoFit: false, // Disable auto-fit
showControls: true, // Show controls
initialZoom: 1.0 // Set desired zoom
};
PDF too large/small:
pdfOptions: PdfOptions = {
autoFit: true, // Let auto-fit handle sizing
initialZoom: undefined // Don't override auto-fit
};
Performance issues:
pdfOptions: PdfOptions = {
renderAnnotationLayer: false, // Disable if not needed
showThumbnails: false // Disable heavy features
};
MIT Β© askinjohn
Made with β€οΈ for the Angular community
FAQs
A modern, zero-configuration PDF viewer for Angular applications with enhanced error handling, auto-fit, text selection, and responsive design
The npm package ng-pdf-renderer receives a total of 13 weekly downloads. As such, ng-pdf-renderer popularity was classified as not popular.
We found that ng-pdf-renderer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.