shumih / ng-print-pdf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NgPrintPdf

The library is available as an npm package. This library uses Mozilla PDF.js to support IE11. It uses native api in other browsers.

Install

To install the package run:

npm i ng-print-pdf

Usage

import PrintPdfModule in target module:

import { PrintPdfModule } from 'ng-print-pdf';

@NgModule({
  imports: [PrintPdfModule]
})
export class AppModule {}

and inject PrintPdfService:

export class PdfService {
  constructor(private printPdfService: PrintPdfService) {}

  public printFile(blob: Blob) {
      return this.printPdfService.printDocument(blob, {
        printResolution: 144,
        scale: 1,
        cssUnits: 78.0 / 72.0,
        layout: 'portrait',
      });
    }
}

Cautions

This solution requires a lot of memory in IE browser. Therefore it's better to add extra check for amount of pages in file.

public async isSafeToPrintPdf(blob: Blob) {
  if (!isIE) {
    return true;
  }

  const amountOfPages = await this.printPdfService.getPagesCount(blob)

  return amountOfPages < 35;
}

About

License:MIT License


Languages

Language:TypeScript 56.4%Language:HTML 39.1%Language:JavaScript 3.7%Language:SCSS 0.8%