selemxmn / ngx-print

:printer: A plug n' play Angular (2++) library to print your stuff
MIT License
167 stars 59 forks source link
angular angular-2 angular4 angular5 angular6 dependency library npm print printing

Coverage Status

ngx-print : plug n' play Angular (2++) directive to print your stuff

This directive makes printing your HTML sections smooth and easy in your Angular application. It is inspired from the old AngularJS ngPrint directive, thus it is intendend to be used with the new Angular -2/4/5/6/7-... Enjoy ! contributions are so welcomed :)

Dependencies

ngx-print Angular
1.2.1 7.0.0 - 14.1.0
1.3.x 15.0.0
1.4.x 16.0.0
1.5.x 17.0.0

Setup

1- In your root application folder run:

$ npm install ngx-print

2- Once ngx-print is installed, you need to import the main module NgxPrintModule :

import {NgxPrintModule} from 'ngx-print';

@NgModule({
  ...
  imports: [NgxPrintModule, ...],
  ...
})
export class YourAppModule {
}

3- Then plug n' play with it:

<div>
  <!--Your html stuff that you want to print-->
</div>
<button>print</button> <!--Your relevant print button-->
 <!--
   1)- Add an ID here
 -->
<div id="print-section"> 
  <!--Your html stuff that you want to print-->
</div>

 <!--
   2)- Add the directive name in your button (ngxPrint),
   3)- Affect your ID to printSectionId
 -->
<button printSectionId="print-section" ngxPrint>print</button> 

Optional properties


<div  id="print-section">

<!-- ... -->

</div>

<button  
    printTitle="MyTitle"  
    printSectionId="print-section"  
    ngxPrint>print</button>

<div  id="print-section">

<!-- ... -->

</div>

<button
    [printStyle]="{h1 : {'color': 'red'}, h2 : {'border': 'solid 1px'}}"
    printSectionId="print-section"
    ngxPrint>print</button>

Here some simple styles were added to every h1 & h2 tags within the div where print-section is tagged to its id attribute.


<div  id="print-section">

<!-- ... -->

</div>

<button
  [useExistingCss]="true"
    printSectionId="print-section"
    ngxPrint>print</button>

<div  id="print-section">

<!-- ... -->

</div>

<button
  styleSheetFile="assets/css/custom1.css,assets/css/custom2.css"
  printSectionId="print-section"
  ngxPrint>print</button>

<div  id="print-section">

<!-- ... -->

</div>

<button
  [previewOnly]="true"
    printSectionId="print-section"
    ngxPrint>print</button>

<div  id="print-section">

<!-- ... -->

</div>

<button
    [closeWindow]="false"
    printSectionId="print-section"
    ngxPrint>print</button>

<div  id="print-section">

<!-- ... -->

</div>

<button
    [bodyClass]="theme-dark"
    printSectionId="print-section"
    ngxPrint>print</button>
<button
    [openNewTab]="true"
    ngxPrint>print</button>

Using NgxPrint as a service (v1.5+)

Inject the NgxPrintService in the constructor of your component or service:

constructor(private printService: NgxPrintService) { }

Printing a Section

import { PrintOptions } from './path-to-your/print-options.model';

printMe() {
  const customPrintOptions: PrintOptions = new PrintOptions({
      printSectionId: 'print-section',
      // Add any other print options as needed
  });
  this.printService.print(customPrintOptions)
}

Print Options Object

The print options object allows you to specify how the print job should be handled. All of which have default values that you can optionally override, although printSectionId is required. It contains the following properties:

  printSectionId: string = null;
  printTitle: string = null;
  useExistingCss: boolean = false;
  bodyClass: string = '';
  openNewTab: boolean = false;
  previewOnly: boolean = false;
  closeWindow: boolean = true;
  printDelay: number = 0;

Setting PrintStyles or StyleSheets

// Optional property for css as a key-value pair
this.printService.printStyle = styleSheet;

// Optional property for a css file location
this.printService.styleSheetFile = fileLocation;

Content-Security-Policy (CSP) Support

If Angular is configured to use a CSP Nonce, ngx-print will automatically inject the [printStyle] CSS rules with this Nonce authorization.

Contributors :1st_place_medal:

Huge thanks to: deeplotia , Ben L , Gavyn McKenzie , silenceway, Muhammad Ahsan Ayaz, Core121 and to all ngx-print users

Donation

Did this project help you reducing time? I won't say no to a cup of coffee 🍵 :)

paypal