ngx-extended-pdf-viewer

repository·main·Indexed 20 days ago

https://github.com/stephanrauh/ngx-extended-pdf-viewer

An Angular wrapper for Mozilla's pdf.js that provides a complete PDF viewer UI and advanced document manipulation capabilities. Version 29 introduces APIs for merging documents, deleting pages, and extracting pages. The library supports Angular 17 through 22, depending on the installed version of ngx-extended-pdf-viewer.

Tokens
39.1K
Snippets
123
Records
165
Agent score
70%

What's inside ngx-extended-pdf-viewer

  1. What is <ngx-extended-pdf-viewer>?

    main
    <ngx-extended-pdf-viewer> is an Angular component that wraps Mozilla's PDF viewer (the same technology used in Google Chrome and Mozilla Firefox). It provides a battle-proven, cross-platform way to display PDF documents within an Angular application, abstracting away the complexities of native browser PDF implementations and ensuring consistent behavior across different platforms.
  2. Choose the right PDF viewer for your Angular application

    main

    When deciding how to display PDF files in an Angular application, consider your requirements for UI complexity and browser support:

    • ngx-extended-pdf-viewer: Best if you need a full-featured, "fancy" UI including a menu, sidebar, and advanced controls. It supports Internet Explorer 11 and provides extensive attributes, events (e.g., document loaded, page rendered), and two-way binding for properties like page number and zoom factor.
    • ng2-pdf-viewer: Best for a "no-nonsense" approach where you only need to render the plain PDF file without a toolbar, thumbnails, or extra UI elements.
    • ng2-pdfjs-viewer: Best if you need to display multiple PDF files side-by-side or open them in new tabs/windows. It uses an iFrame-based approach to wrap the PDF viewer, allowing parameter passing via the URL.
    • Native Browser Support: Use the <object> tag for a lightweight solution if you do not need to support older browsers like Internet Explorer or require a standardized embedding API.
    • Commercial Solutions: If you need to modify PDF files (editing content), look into professional tools like PDFTron or the commercial version of ej2-angular-pdfviewer.
  3. How the secondary menu and responsive design work

    main

    Since version 18, ngx-extended-pdf-viewer supports dynamic movement of buttons between the main toolbar and a secondary menu based on screen size. This ensures that all buttons remain accessible even on small displays.

    Breakpoint Logic

    Every toolbar button uses a show<ButtonName> attribute (or CSS classes for custom buttons) to define its visibility breakpoint.

    • always-visible: The button is never moved to the secondary menu.
    • always-in-secondary-menu: The button always stays in the secondary menu.
    • Breakpoint values: Using standard sizes like xxs, xs, sm, md, lg, xl, and xxl determines when the button shifts from the toolbar to the menu. For example, xxl means the button only appears in the toolbar on very large screens.
  4. Key advantages of ngx-extended-pdf-viewer

    main

    Compared to other Angular PDF libraries, ngx-extended-pdf-viewer provides:

    1. Advanced UI: Includes a full menu, sidebar, and comprehensive user controls.
    2. Browser Compatibility: Supports Internet Explorer 11.
    3. Rich API: Offers a wide range of attributes and events, such as triggering actions when a document is loaded or a specific page is rendered.
    4. Two-way Binding: Supports two-way data binding for attributes like pageNumber and zoomFactor, allowing you to easily persist and restore user preferences (e.g., saving a user's zoom level to a database).
  5. How the User Find Controller and API Find Controller differ

    main

    The ngx-extended-pdf-viewer provides two distinct ways to programmatically trigger search functionality via the NgxExtendedPdfViewerService:

    1. User Find Controller: This is part of the standard UI. When used, the search state is reflected in the viewer's UI components. Use this when you want the user to see the search bar and current find status.
    2. API Find Controller (Secondary Find Controller): This is designed for programmatic use. It allows developers to perform searches without interfering with the user's own manual search activity in the UI.

    Both are accessed by injecting NgxExtendedPdfViewerService and calling the .find() method, toggling the useSecondaryFindcontroller option.

    // Use User Find Controller (UI reflected)
    this.ngxExtendedPdfViewerService.find(searchText, {
      useSecondaryFindcontroller: false
    });
    
    // Use API Find Controller (Programmatic, non-interfering)
    this.ngxExtendedPdfViewerService.find(searchText, {
      useSecondaryFindcontroller: true
    });
  6. Fine-grained thumbnail customization with (thumbnailDrawn)

    main

    For logic-based customization that goes beyond static HTML (such as changing colors based on page content or adding event listeners), use the (thumbnailDrawn) event.

    This event provides a PdfThumbnailDrawnEvent object containing the thumbnail DOM element. You can use standard DOM APIs (querySelector, style, classList) to manipulate the thumbnail after it has been rendered.

    Calling Angular Code from (thumbnailDrawn): Because the thumbnail drawing happens outside of Angular's standard change detection cycle, if you modify component state inside the (thumbnailDrawn) handler, you must manually trigger change detection using ChangeDetectorRef.detectChanges() or ChangeDetectorRef.markForCheck() (unless you are using ngZone).

    public onThumbnailDrawn(thumbnailEvent: PdfThumbnailDrawnEvent): void {
      const thumbnail = thumbnailEvent.thumbnail;
      const page = thumbnailEvent.pageNumber; // Access page number from event
    
      // Example: Manipulate DOM directly
      const overlay = thumbnail.querySelector('.image-container') as HTMLElement;
      if (page <= 2) {
        overlay.style.backgroundColor = '#0000FF40';
      }
    
      // Example: Add native DOM event listeners
      overlay.ondblclick = () => {
        this.performAngularAction();
      };
    }
  7. Understand the different rendering layers in ngx-extended-pdf-viewer

    main

    The ngx-extended-pdf-viewer renders the PDF content using a base HTML5 <canvas>. To provide interactive features, it overlays additional HTML <div> layers on top of this canvas.

    There are three primary types of layers you can interact with or configure:

    1. Text Layer: An HTML layer used for text selection and accessibility.
    2. Annotation Layer: An HTML layer that handles interactive elements like links and form fields.
    3. PDF Layers: Additional layers specific to PDF features.

    While these layers are managed automatically, they are rendered as standard HTML <div> elements, meaning they can be targeted and manipulated via CSS or DOM APIs if necessary for custom styling or behavior.

  8. Enable chunked loading via Range Requests

    main

    By default, ngx-extended-pdf-viewer uses Range Requests to load large PDF files in chunks rather than downloading the entire file at once. This allows the viewer to display the first page while other pages are still being streamed.

    To ensure this works, your server must support range requests and include the following response headers:

    • Content-Length
    • Accept-Ranges: "bytes"
    • Content-Encoding