Skip to content

Commit

Permalink
Merge pull request #176 from selemxmn/open-new-tab-option
Browse files Browse the repository at this point in the history
Adds open new tab option
  • Loading branch information
broem committed Dec 7, 2023
2 parents 8b6d4f0 + 3f44572 commit eeea42f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/lib/ngx-print.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,35 @@ export class PrintBase {
*/
protected print(printOptions: PrintOptions): void {

let styles = '', links = '';
let styles = '', links = '', popOut = 'top=0,left=0,height=auto,width=auto';
const baseTag = this.getElementTag('base');

if (printOptions.useExistingCss) {
styles = this.getElementTag('style');
links = this.getElementTag('link');
}

// If the openNewTab option is set to true, then set the popOut option to an empty string.
// This will cause the print dialog to open in a new tab.
if (printOptions.openNewTab) {
popOut = '';
}

const printContents = this.getHtmlContents(printOptions.printSectionId);
if (!printContents) {
// Handle the case where the specified print section is not found.
console.error(`Print section with id ${printOptions.printSectionId} not found.`);
return;
}

const popupWin = window.open("", "_blank", "top=0,left=0,height=auto,width=auto");
const popupWin = window.open("", "_blank", popOut);

if (!popupWin) {
// the popup window could not be opened.
console.error('Could not open print window.');
return;
}

popupWin.document.open();
popupWin.document.write(`
<html>
Expand Down
8 changes: 8 additions & 0 deletions src/lib/ngx-print.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export class NgxPrintDirective extends PrintBase {
this.printOptions = { ...this.printOptions, bodyClass: value };
}

/**
* Whether to open a new window or default to new window.
*
*/
@Input() set openNewTab(value: boolean) {
this.printOptions = { ...this.printOptions, openNewTab: value };
}

/**
*
*
Expand Down
13 changes: 13 additions & 0 deletions src/lib/ngx-print.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ describe('NgxPrintService', () => {
expect(service.print).toHaveBeenCalledWith(customPrintOptions);
});

it('should open new tab', () => {
spyOn(service, 'print');

const customPrintOptions: PrintOptions = new PrintOptions({
printSectionId: 'print-section',
openNewTab: true
});

component.printMe(customPrintOptions);

expect(service.print).toHaveBeenCalledWith(customPrintOptions);
});

it('should test the printStyle', () => {

// Create a spy on the instance's method
Expand Down
1 change: 1 addition & 0 deletions src/lib/print-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export class PrintOptions {
printTitle: string = null;
useExistingCss: boolean = false;
bodyClass: string = '';
openNewTab: boolean = false;
previewOnly: boolean = false;
closeWindow: boolean = true;
printDelay: number = 0;
Expand Down

0 comments on commit eeea42f

Please sign in to comment.