Search code examples
google-sheetsgoogle-apps-script

How to Merge PDF Pages Into One Long Page in Appscript


I transfer the information I entered via Google form to pdf via Google Document. After the form is submitted, a lot of information is automatically generated through Google Documents and the page layout is distorted according to the photo sizes, so I want to turn all the pages into a single long page as a PDF. How can i Merge PDF Pages Into One Long Page in Apps Script ?

  function myFunction() {
  var docFile = DriveApp.getFileById('1cc1xjtE6WyYjVVwY95earN6xht2mP01EEPhAyC-yFHE');
  var TempFolder = DriveApp.getFolderById('1vaXCrOJAg1x2JCzEng9nSPn6om8dWmib');
  var PDFFolder = DriveApp.getFolderById('16r-564UEEuBcyfTIX0xnjJRk6bFJjzL1');
  var ss = SpreadsheetApp.openById('1kZFrbnjgVnq7wB4mFYAMCjFiDB2PAfkmbYGvhLg_U00');

  var sh = ss.getSheetByName('Form Yanıtları 1');
  var data = sh.getRange(ss.getLastRow(), 1, 1, 48).getValues();
  data.forEach(r => {
    var item1 = r[1]; 
    var item2 = r[2]; 
    var item3 = r[3]; 
    var item4 = r[4]; 
    var item5 = r[5]; 
    var item6 = r[6]; 
    var item7 = r[7]; 
    var item8 = r[8]; 
    var item9 = r[9]; 
    var item10 = r[10]; 
    var item12 = r[12]; 
    var item13 = r[13]; 
    var item16 = r[16]; 
    var item19 = r[19]; 
    var item22 = r[22]; 
    var item25 = r[25];
    var item28 = r[28];
    var item31 = r[31];
    var item34 = r[34];
    var item37 = r[37];
    var item40 = r[40];
    var item42 = r[42];
    var item44 = r[44];
    item6 = LanguageApp.translate(Utilities.formatDate(item6, 'GMT+7', 'dd MMMM yyyy'), 'en', 'tr').split(' ').map((a, i) => { if (i != 2 || parseInt(a) > 2100) { return a }; a = parseInt(a); return a }).join(' ');
    item7 = Utilities.formatDate(item7, 'GMT+3', 'HH:mm'); // Denetim başlama saati
    item8 = Utilities.formatDate(item8, 'GMT+3', 'HH:mm'); // Denetim bitiş saati

    CreatePDF(docFile, TempFolder, PDFFolder, item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, 
    r[11].split("=")[1], 
    item12, 
    item13, 
    r[14].split("=")[1], 
    r[15].split("=")[1], 
    item16, 
    r[17].split("=")[1], 
    r[18].split("=")[1],
    item19, 
    r[20].split("=")[1], 
    r[21].split("=")[1],
    item22, 
    r[23].split("=")[1], 
    r[24].split("=")[1],
    item25, 
    r[26].split("=")[1], 
    r[27].split("=")[1],
    item28, 
    r[29].split("=")[1], 
    r[30].split("=")[1],
    item31, 
    r[32].split("=")[1], 
    r[33].split("=")[1],
    item34, 
    r[35].split("=")[1], 
    r[36].split("=")[1],
    item37, 
    r[38].split("=")[1], 
    r[39].split("=")[1],
    item40, 
    r[41].split("=")[1],
    item42, 
    r[43].split("=")[1],
    item44, 
    r[45].split("=")[1], 
    r[46].split("=")[1], 
    r[47].split("=")[1]);
  });
    
}

function CreatePDF(docFile, TempFolder, PDFFolder, item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, id1, item12, item13, id2,id3, item16,id4,id5,item19,id6,id7,item22,id8,id9,item25,id10,id11,item28,id12,id13,item31,id14,id15,item34,id16,id17,item37,id18,id19,item40,id20,item42,id21,item44,id22,id23,id24) {
  var tempFile = docFile.makeCopy(TempFolder);
  var tempDoc = DocumentApp.openById(tempFile.getId());
  var body = tempDoc.getBody();
  body.replaceText("{MAĞAZA ADI}", item2);
  body.replaceText("{DENETİMİ YAPAN}", item3);
  body.replaceText("{İLGİLİ HAFTA}", item4);
  body.replaceText("{MAĞAZA PUANI}", item5);
  body.replaceText("{DENETİM TARİHİ}", item6);
  body.replaceText("{DENETİM BAŞLAMA SAATİ}", item7);
  body.replaceText("{DENETİM BİTİŞ SAATİ}", item8);
  body.replaceText("{BİR ÖNCEKİ DENETİMİ YAPAN}", item9);
  body.replaceText("{MAĞAZANIN MESAİ PROGRAMI UYGUN MU}", item10);
  body.replaceText("{PERSONEL TUTUM VE DAVRANIŞLARI}", item12);
  body.replaceText("{KAHVE BÖLÜMÜ GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item13);
  body.replaceText("{KURUYEMİŞ BÖLÜMÜ GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item16);
  body.replaceText("{LOKUM BÖLÜMÜ GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item19);
  body.replaceText("{BAHARAT BÖLÜMÜ GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item22);
  body.replaceText("{ORTA ALAN GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item25);
  body.replaceText("{MAĞAZA ÖNÜ VE LEBLEBİ BÖLÜMÜ GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item28);
  body.replaceText("{KASA BÖLÜMÜ GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item31);
  body.replaceText("{DEPO BÖLÜMÜ GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item34);
  body.replaceText("{MAĞAZA TEMİZLİK GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item37);
  body.replaceText("{MAĞAZADA ZAİYAT BULUNUYOR MU GÖRÜŞ VE ÖNERİLERİNİZ NELERDİR}", item40);
  body.replaceText("{BİR ÖNCEKİ HAFTA BİLGİLENDİRMELERİ İŞLENMİŞ Mİ}", item42);
  body.replaceText("{DİĞER YORUMLAR}", item44);
  var replaceTextToImage = function (body, searchText, image, width) {
  var next = body.findText(searchText);
  if (!next) return;
  var r = next.getElement();
  r.asText().setText("");
  var img = r.getParent().asParagraph().insertInlineImage(0, image);
  if (width && typeof width == "number") {
    var w = img.getWidth();
    var h = img.getHeight();
    var newWidth = width;
    var newHeight = (width * h) / w;
    if (h > w) {
      newHeight = width;
      newWidth = (width * w) / h;
    }
    img.setWidth(newWidth);
    img.setHeight(newHeight);
  }
  return next;
};

  replaceTextToImage(body, "{IMAGE1}", DriveApp.getFileById(id1).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE2}", DriveApp.getFileById(id2).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE3}", DriveApp.getFileById(id3).getBlob(), 400);
  replaceTextToImage(body, "{IMAGE4}", DriveApp.getFileById(id4).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE5}", DriveApp.getFileById(id5).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE6}", DriveApp.getFileById(id6).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE7}", DriveApp.getFileById(id7).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE8}", DriveApp.getFileById(id8).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE9}", DriveApp.getFileById(id9).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE10}", DriveApp.getFileById(id10).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE11}", DriveApp.getFileById(id11).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE12}", DriveApp.getFileById(id12).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE13}", DriveApp.getFileById(id13).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE14}", DriveApp.getFileById(id14).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE15}", DriveApp.getFileById(id15).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE16}", DriveApp.getFileById(id16).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE17}", DriveApp.getFileById(id17).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE18}", DriveApp.getFileById(id18).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE19}", DriveApp.getFileById(id19).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE20}", DriveApp.getFileById(id20).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE21}", DriveApp.getFileById(id21).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE22}", DriveApp.getFileById(id22).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE23}", DriveApp.getFileById(id23).getBlob(), 500);
  replaceTextToImage(body, "{IMAGE24}", DriveApp.getFileById(id24).getBlob(), 500);

  tempDoc.saveAndClose()
  tempFile.setTrashed(true);
  
  var PdfContent = tempFile.getAs(MimeType.PDF);
  var PdfFile = PDFFolder.createFile(PdfContent).setName(item2 + ' ' + item6 + ' DENETİM RAPORU');
  MailApp.sendEmail(item1, item2 + ' ŞUBE', 'DENETİM FORMU..', { attachments: [PdfFile.getBlob()] });
} 

Solution

  • I believe your goal is as follows.

    • You want to increase the page height using Google Apps Script.

    In that case, how about adjusting the page height of the Google Document?

    Pattern 1:

    As a simple modification, if you can directly change the page height of the template Document, how about manually increasing the page height of the template Document? In this case, your script can be used without modification.

    Pattern 2:

    If your showing script is modified, please modify your function CreatePDF as follows. In this case, the page height of the copied Document is changed. The original Document is not changed.

    From:

    var body = tempDoc.getBody();
    

    To:

    var body = tempDoc.getBody().setPageHeight(10000);
    
    • It seems that the default value is 841.89 points. In this modification 10000 is used to the page height. With this modification, the page height of one page is expanded. So, when this Document is exported as PDF data, the PDF data has a large page height.
    • In this modification 10000 is a sample value. Please adjust the value to your actual situation.
    • If you want to also adjust the page width, please modify var body = tempDoc.getBody().setPageHeight(10000) to var body = tempDoc.getBody().setPageHeight(10000).setPageWidth(width). Please set the values of height and width.

    References: