function makePDF(print = false) {
  const SQUARE_SIZE = parseInt(
    document.querySelector("[name=size]:checked").value
  );
  const PAPER_SEL = document.querySelector("#paper option:checked");
  const FORMAT = PAPER_SEL.value;
  const PAGE_WIDTH = parseFloat(PAPER_SEL.dataset.pagewidth);
  const MAX_WIDTH = parseInt(PAPER_SEL.dataset.contentwidth);
  const OVERLAP = parseInt(document.querySelector("#overlap").value);

  const PDF = new jsPDF({unit: "cm", format: FORMAT, compress: false});

  /**
    * @desc Adds image to PDF as seperate page(s)
    * @param image image - image to be added.
    * @param i int - number of image (1-8).
  */
  function processImage(image, i) {
    const TITLES = [
      "Top Left", "Top Center", "Top Right",
      "Middle Left", "Middle Center", "Middle Right",
      "Bottom Left", "Bottom Center", "Bottom Right"
    ];
    let title = TITLES[i];
    let titleWidth = PDF.getTextWidth(title);

    if (i > 0) {
      PDF.addPage({format: FORMAT});
    }

    PDF.setFontSize(8);
    PDF.text(title, 1.25, 1.25 + titleWidth, {angle: 90});
    addSquare(PDF, image, 0, MAX_WIDTH);

    if (SQUARE_SIZE > MAX_WIDTH) {
      const SLICE_WIDTH = MAX_WIDTH - OVERLAP;

      PDF.addPage({format: FORMAT});

      title = `${TITLES[i]} (Part 2)`;
      titleWidth = PDF.getTextWidth(title);

      PDF.text(title, 1.25, 1.25 + titleWidth, {angle: 90});
      addSquare(PDF, image, SLICE_WIDTH, SQUARE_SIZE - SLICE_WIDTH);
    }
  }

  /**
    * @desc Finishes PDF processing
  */
  function closePDF() {
    if (print) {
      PDF.autoPrint();
      document.querySelector("#printer").src = PDF.output("bloburl");
    } else {
      PDF.save("tictactoe.pdf");
    }
    document.querySelector(".loader").classList.remove("show");
  }

  /**
    * @desc Adds all images to PDF as a single page
  */
  function makeSinglePage() {
    const LEFT_ORIGIN = (PAGE_WIDTH / 2) - 4;
    const TOP_ORIGIN = 1.25;
    const OUTLINE_WIDTH = 0.02;

    for (let col = 0; col < 3; col++) {
      for (let row = 0; row < 3; row++) {
        let x = LEFT_ORIGIN + (3 * row);
        let y = TOP_ORIGIN + (3 * col);
        let image = document.querySelector(
          `.square:nth-child(${1 + row + (col * 3)})`
        );

        PDF.setFillColor(204, 204, 204);
        PDF.rect(
          x - OUTLINE_WIDTH,
          y - OUTLINE_WIDTH,
          2 + (2 * OUTLINE_WIDTH),
          2 + (2 * OUTLINE_WIDTH),
          "F"
        );

        PDF.addImage(image, "JPEG", x, y, 2, 2, "", "NONE");
      }
    }

    closePDF();
  }


  /**
    * @desc Throttles makePDF to avoid locking main thread too long
    * @param num int - number of image to be processed (0-8)
  */
  function throttledMakePDF(num) {
    let img = document.querySelector(`.square:nth-child(${num + 1})`);
    processImage(img, num);

    if (num < 8 ) {
      requestAnimationFrame(() => throttledMakePDF(num + 1));
    } else {
      closePDF();
    }
  }


  if (SQUARE_SIZE === 2) {
      makeSinglePage();
  } else {
    requestAnimationFrame(() => throttledMakePDF(0));
  }
}
/**
  * @desc Creates PDF file
  * @param print string - if true, prints file. Otherwise saves.
*/
@desc Creates PDF file
@param print string - if true, prints file. Otherwise saves.
@desc Creates PDF file @param print string - if true prints file . Otherwise saves .
function makePDF(print = false) {
  const SQUARE_SIZE = parseInt(
    document.querySelector("[name=size]:checked").value
  );
  const PAPER_SEL = document.querySelector("#paper option:checked");
  const FORMAT = PAPER_SEL.value;
  const PAGE_WIDTH = parseFloat(PAPER_SEL.dataset.pagewidth);
  const MAX_WIDTH = parseInt(PAPER_SEL.dataset.contentwidth);
  const OVERLAP = parseInt(document.querySelector("#overlap").value);
  const PDF = new jsPDF({unit: "cm", format: FORMAT, compress: false});
  function processImage(image, i) {
    const TITLES = [
      "Top Left", "Top Center", "Top Right",
      "Middle Left", "Middle Center", "Middle Right",
      "Bottom Left", "Bottom Center", "Bottom Right"
    ];
    let title = TITLES[i];
    let titleWidth = PDF.getTextWidth(title);
    if (i > 0) {
      PDF.addPage({format: FORMAT});
    }
    PDF.setFontSize(8);
    PDF.text(title, 1.25, 1.25 + titleWidth, {angle: 90});
    addSquare(PDF, image, 0, MAX_WIDTH);
    if (SQUARE_SIZE > MAX_WIDTH) {
      const SLICE_WIDTH = MAX_WIDTH - OVERLAP;
      PDF.addPage({format: FORMAT});
      title = `${TITLES[i]} (Part 2)`;
      titleWidth = PDF.getTextWidth(title);
      PDF.text(title, 1.25, 1.25 + titleWidth, {angle: 90});
      addSquare(PDF, image, SLICE_WIDTH, SQUARE_SIZE - SLICE_WIDTH);
    }
  }
  function closePDF() {
    if (print) {
      PDF.autoPrint();
      document.querySelector("#printer").src = PDF.output("bloburl");
    } else {
      PDF.save("tictactoe.pdf");
    }
    document.querySelector(".loader").classList.remove("show");
  }
  function makeSinglePage() {
    const LEFT_ORIGIN = (PAGE_WIDTH / 2) - 4;
    const TOP_ORIGIN = 1.25;
    const OUTLINE_WIDTH = 0.02;
    for (let col = 0; col < 3; col++) {
      for (let row = 0; row < 3; row++) {
        let x = LEFT_ORIGIN + (3 * row);
        let y = TOP_ORIGIN + (3 * col);
        let image = document.querySelector(
          `.square:nth-child(${1 + row + (col * 3)})`
        );
        PDF.setFillColor(204, 204, 204);
        PDF.rect(
          x - OUTLINE_WIDTH,
          y - OUTLINE_WIDTH,
          2 + (2 * OUTLINE_WIDTH),
          2 + (2 * OUTLINE_WIDTH),
          "F"
        );
        PDF.addImage(image, "JPEG", x, y, 2, 2, "", "NONE");
      }
    }
    closePDF();
  }
  function throttledMakePDF(num) {
    let img = document.querySelector(`.square:nth-child(${num + 1})`);
    processImage(img, num);
    if (num < 8 ) {
      requestAnimationFrame(() => throttledMakePDF(num + 1));
    } else {
      closePDF();
    }
  }
  if (SQUARE_SIZE === 2) {
      makeSinglePage();
  } else {
    requestAnimationFrame(() => throttledMakePDF(0));
  }
}
function makePDF ( print = false ) { const SQUARE_SIZE = parseInt ( document . querySelector ( "[name=size]:checked" ) . value ) ; const PAPER_SEL = document . querySelector ( "#paper option:checked" ) ; const FORMAT = PAPER_SEL . value ; const PAGE_WIDTH = parseFloat ( PAPER_SEL . dataset . pagewidth ) ; const MAX_WIDTH = parseInt ( PAPER_SEL . dataset . contentwidth ) ; const OVERLAP = parseInt ( document . querySelector ( "#overlap" ) . value ) ; const PDF = new jsPDF ( { unit : "cm" , format : FORMAT , compress : false } ) ; function processImage ( image , i ) { const TITLES = [ "Top Left" , "Top Center" , "Top Right" , "Middle Left" , "Middle Center" , "Middle Right" , "Bottom Left" , "Bottom Center" , "Bottom Right" ] ; let title = TITLES [ i ] ; let titleWidth = PDF . getTextWidth ( title ) ; if ( i > 0 ) { PDF . addPage ( { format : FORMAT } ) ; } PDF . setFontSize ( 8 ) ; PDF . text ( title , 1.25 , 1.25 + titleWidth , { angle : 90 } ) ; addSquare ( PDF , image , 0 , MAX_WIDTH ) ; if ( SQUARE_SIZE > MAX_WIDTH ) { const SLICE_WIDTH = MAX_WIDTH - OVERLAP ; PDF . addPage ( { format : FORMAT } ) ; title = ` ${ TITLES [ i ] } ` ; titleWidth = PDF . getTextWidth ( title ) ; PDF . text ( title , 1.25 , 1.25 + titleWidth , { angle : 90 } ) ; addSquare ( PDF , image , SLICE_WIDTH , SQUARE_SIZE - SLICE_WIDTH ) ; } } function closePDF ( ) { if ( print ) { PDF . autoPrint ( ) ; document . querySelector ( "#printer" ) . src = PDF . output ( "bloburl" ) ; } else { PDF . save ( "tictactoe.pdf" ) ; } document . querySelector ( ".loader" ) . classList . remove ( "show" ) ; } function makeSinglePage ( ) { const LEFT_ORIGIN = ( PAGE_WIDTH / 2 ) - 4 ; const TOP_ORIGIN = 1.25 ; const OUTLINE_WIDTH = 0.02 ; for ( let col = 0 ; col < 3 ; col ++ ) { for ( let row = 0 ; row < 3 ; row ++ ) { let x = LEFT_ORIGIN + ( 3 * row ) ; let y = TOP_ORIGIN + ( 3 * col ) ; let image = document . querySelector ( ` ${ 1 + row + ( col * 3 ) } ` ) ; PDF . setFillColor ( 204 , 204 , 204 ) ; PDF . rect ( x - OUTLINE_WIDTH , y - OUTLINE_WIDTH , 2 + ( 2 * OUTLINE_WIDTH ) , 2 + ( 2 * OUTLINE_WIDTH ) , "F" ) ; PDF . addImage ( image , "JPEG" , x , y , 2 , 2 , "" , "NONE" ) ; } } closePDF ( ) ; } function throttledMakePDF ( num ) { let img = document . querySelector ( ` ${ num + 1 } ` ) ; processImage ( img , num ) ; if ( num < 8 ) { requestAnimationFrame ( ( ) => throttledMakePDF ( num + 1 ) ) ; } else { closePDF ( ) ; } } if ( SQUARE_SIZE === 2 ) { makeSinglePage ( ) ; } else { requestAnimationFrame ( ( ) => throttledMakePDF ( 0 ) ) ; } }
@desc Creates PDF file
 @param print string - if true, prints file.
@desc Creates PDF file @param print string - if true prints file .
/**
    * @desc Adds image to PDF as seperate page(s)
    * @param image image - image to be added.
    * @param i int - number of image (1-8).
  */ /**
    * @desc Finishes PDF processing
  */ /**
    * @desc Adds all images to PDF as a single page
  */ /**
    * @desc Throttles makePDF to avoid locking main thread too long
    * @param num int - number of image to be processed (0-8)
  */