google-apigoogle-cloud-print

Google Script - settings to print pdf in label size (3.5 inch x 2.2 inch)


I am generating a PDF document using google script on submit of google form. The idea is to print the name entered in the form on a label using a Label Printer. The printer is a google cloud printer.

function getNameOnSubmit() {
  var formResponses = FormApp.getActiveForm().getResponses();
  var formResponse = formResponses[formResponses.length-1];
  var itemResponses = formResponse.getItemResponses();

    var itemResponse = itemResponses[0];
    Logger.log('Last response to the question "%s" was "%s"',
               itemResponse.getItem().getTitle(),
               itemResponse.getResponse());
 createPDFFile(itemResponse.getResponse());
}

function createPDFFile(response) {
  var docName = "Template";
  var docTemplate = "1W-asdasdasdasdasd1398612983";

  var tz = Session.getScriptTimeZone();
  var timeStamp = Utilities.formatDate(new Date(), tz, 'ddMMyyyyhhmmss');

 // Get document template, copy it as a new temp doc, and save the Doc’s id
  var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName+'_'+ timeStamp).getId();

 // Open the temporary document
  var copyDoc = DocumentApp.openById(copyId);

 // Get the document’s body section
  var copyBody = copyDoc.getActiveSection();

  // Replace place holder keys located in our google doc template
  copyBody.replaceText('Name', response);

  // Save and close the temporary document
  copyDoc.saveAndClose();

  // Convert temporary document to PDF (XYZ-TEMP)
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf"); 
  // Convert temporary document to PDF
  var pdf = DriveApp.getFileById(copyId);
  var theblob = pdf.getBlob().getAs('application/pdf'); 
  var folder = DriveApp.getFolderById('24234234524sgsdfgsgsgsd'); 
  var movefile = folder.createFile(theblob);
   // Delete temp file
  DriveApp.getFileById(copyId).setTrashed(true);

  printNameFromSheets(movefile.getId(),"43543543regsdfgsdfgsdf", movefile.getName());
}

function printNameFromSheets(docID, printerID, docName) {
  var ticket = {
    version: "1.0",
    print: {
      color: {
        type: "STANDARD_COLOR",
        //STANDARD_MONOCHROME 
        vendor_id: "Color"
      },
      duplex: {
        type: "NO_DUPLEX"
      },
      fit_to_page:{
        type: "NO_FITTING"
      },
      media_size: {
          width_microns: "88900",
          height_microns: "55880",
          is_continuous_feed: "false"
      }
    }
  };

Logger.log(' 43 '+JSON.stringify(ticket));

  var payload = {
    "printerid" : printerID,
    "title"     : docName,
    "content"   : DriveApp.getFileById(docID).getBlob(),
    "contentType": "application/pdf",
    "ticket"    : JSON.stringify(ticket)
  };

  var response = UrlFetchApp.fetch('https://www.google.com/cloudprint/submit', {
    method: "POST",
    payload: payload,
    headers: {
      Authorization: 'Bearer ' + getCloudPrintService().getAccessToken()
    },
    "muteHttpExceptions": true
  });

  response = JSON.parse(response);

  if (response.success) {
    Logger.log("%s", response.message);
  } else {
    Logger.log("Error Code: %s %s", response.errorCode, response.message);
  }
}

The function works fine until I add the below configuration to the code to adjust the size of the print

  fit_to_page:{
    type: "NO_FITTING"
  },
  media_size: {
      width_microns: "88900",
      height_microns: "55880",
      is_continuous_feed: "false"
  }

This is what I get in Logs [18-01-16 10:41:12:154] Last response to the question "Name" was "Test Test" [18-01-16 10:41:19:067] 43 {"version":"1.0","print":{"color":{"type":"STANDARD_COLOR","vendor_id":"Color"},"duplex":{"type":"NO_DUPLEX"},"fit_to_page":{"type":"NO_FITTING"},"media_size":{"width_microns":"88900","height_microns":"55880","is_continuous_feed":"false"}}} [18-01-16 10:41:19:657] Error Code: 424.0 Failed to parse the print job's print ticket.


Thanks in advance.


Solution

  • How about the following modification? false is boolean. So please remove ".

    From :

    is_continuous_feed: "false"
    

    To :

    is_continuous_feed: false
    

    If this didn't work, I'm sorry.