Slightly modernize the print layout-methods
				
					
				
			By getting the width/height of the first page initially, we can slightly reduce the amount of code needed both in the `hasEqualPageSizes`-check and when building the print-styles.
This commit is contained in:
		
							parent
							
								
									342dc760da
								
							
						
					
					
						commit
						ebf493f726
					
				@ -146,16 +146,13 @@ FirefoxPrintService.prototype = {
 | 
				
			|||||||
    const body = document.querySelector("body");
 | 
					    const body = document.querySelector("body");
 | 
				
			||||||
    body.setAttribute("data-pdfjsprinting", true);
 | 
					    body.setAttribute("data-pdfjsprinting", true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const hasEqualPageSizes = this.pagesOverview.every(function (size) {
 | 
					    const { width, height } = this.pagesOverview[0];
 | 
				
			||||||
      return (
 | 
					    const hasEqualPageSizes = this.pagesOverview.every(
 | 
				
			||||||
        size.width === this.pagesOverview[0].width &&
 | 
					      size => size.width === width && size.height === height
 | 
				
			||||||
        size.height === this.pagesOverview[0].height
 | 
					    );
 | 
				
			||||||
      );
 | 
					 | 
				
			||||||
    }, this);
 | 
					 | 
				
			||||||
    if (!hasEqualPageSizes) {
 | 
					    if (!hasEqualPageSizes) {
 | 
				
			||||||
      console.warn(
 | 
					      console.warn(
 | 
				
			||||||
        "Not all pages have the same size. The printed " +
 | 
					        "Not all pages have the same size. The printed result may be incorrect!"
 | 
				
			||||||
          "result may be incorrect!"
 | 
					 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -163,8 +160,7 @@ FirefoxPrintService.prototype = {
 | 
				
			|||||||
    // set. Note that we assume that all pages have the same size, because
 | 
					    // set. Note that we assume that all pages have the same size, because
 | 
				
			||||||
    // variable-size pages are scaled down to the initial page size in Firefox.
 | 
					    // variable-size pages are scaled down to the initial page size in Firefox.
 | 
				
			||||||
    this.pageStyleSheet = document.createElement("style");
 | 
					    this.pageStyleSheet = document.createElement("style");
 | 
				
			||||||
    const pageSize = this.pagesOverview[0];
 | 
					    this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`;
 | 
				
			||||||
    this.pageStyleSheet.textContent = `@page { size: ${pageSize.width}pt ${pageSize.height}pt;}`;
 | 
					 | 
				
			||||||
    body.append(this.pageStyleSheet);
 | 
					    body.append(this.pageStyleSheet);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (pdfDocument.isPureXfa) {
 | 
					    if (pdfDocument.isPureXfa) {
 | 
				
			||||||
 | 
				
			|||||||
@ -92,16 +92,13 @@ PDFPrintService.prototype = {
 | 
				
			|||||||
    const body = document.querySelector("body");
 | 
					    const body = document.querySelector("body");
 | 
				
			||||||
    body.setAttribute("data-pdfjsprinting", true);
 | 
					    body.setAttribute("data-pdfjsprinting", true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const hasEqualPageSizes = this.pagesOverview.every(function (size) {
 | 
					    const { width, height } = this.pagesOverview[0];
 | 
				
			||||||
      return (
 | 
					    const hasEqualPageSizes = this.pagesOverview.every(
 | 
				
			||||||
        size.width === this.pagesOverview[0].width &&
 | 
					      size => size.width === width && size.height === height
 | 
				
			||||||
        size.height === this.pagesOverview[0].height
 | 
					    );
 | 
				
			||||||
      );
 | 
					 | 
				
			||||||
    }, this);
 | 
					 | 
				
			||||||
    if (!hasEqualPageSizes) {
 | 
					    if (!hasEqualPageSizes) {
 | 
				
			||||||
      console.warn(
 | 
					      console.warn(
 | 
				
			||||||
        "Not all pages have the same size. The printed " +
 | 
					        "Not all pages have the same size. The printed result may be incorrect!"
 | 
				
			||||||
          "result may be incorrect!"
 | 
					 | 
				
			||||||
      );
 | 
					      );
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -115,9 +112,7 @@ PDFPrintService.prototype = {
 | 
				
			|||||||
    // will be ignored and the user has to select the correct paper size in
 | 
					    // will be ignored and the user has to select the correct paper size in
 | 
				
			||||||
    // the UI if wanted.
 | 
					    // the UI if wanted.
 | 
				
			||||||
    this.pageStyleSheet = document.createElement("style");
 | 
					    this.pageStyleSheet = document.createElement("style");
 | 
				
			||||||
    const pageSize = this.pagesOverview[0];
 | 
					    this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`;
 | 
				
			||||||
    this.pageStyleSheet.textContent =
 | 
					 | 
				
			||||||
      "@page { size: " + pageSize.width + "pt " + pageSize.height + "pt;}";
 | 
					 | 
				
			||||||
    body.append(this.pageStyleSheet);
 | 
					    body.append(this.pageStyleSheet);
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user