Google 脚本 - 设置以标签尺寸(3.5 英寸 x 2.2 英寸)打印 pdf
Google Script - settings to print pdf in label size (3.5 inch x 2.2 inch)
我在提交 google 表单时使用 google 脚本生成 PDF 文档。
这个想法是使用标签打印机将表单中输入的名称打印在标签上。打印机是 google 云打印机。
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);
}
}
在我将以下配置添加到代码以调整打印大小之前,该功能工作正常
fit_to_page:{
type: "NO_FITTING"
},
media_size: {
width_microns: "88900",
height_microns: "55880",
is_continuous_feed: "false"
}
这是我在日志中得到的
[18-01-16 10:41:12:154] 对问题 "Name" 的最后回复是 "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] 错误代码:424.0 无法解析打印作业的打印票。
提前致谢。
下面的修改怎么样? false
是布尔值。所以请删除 "
.
发件人:
is_continuous_feed: "false"
收件人:
is_continuous_feed: false
如果这不起作用,我很抱歉。
我在提交 google 表单时使用 google 脚本生成 PDF 文档。 这个想法是使用标签打印机将表单中输入的名称打印在标签上。打印机是 google 云打印机。
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);
}
}
在我将以下配置添加到代码以调整打印大小之前,该功能工作正常
fit_to_page:{
type: "NO_FITTING"
},
media_size: {
width_microns: "88900",
height_microns: "55880",
is_continuous_feed: "false"
}
这是我在日志中得到的 [18-01-16 10:41:12:154] 对问题 "Name" 的最后回复是 "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] 错误代码:424.0 无法解析打印作业的打印票。
提前致谢。
下面的修改怎么样? false
是布尔值。所以请删除 "
.
发件人:
is_continuous_feed: "false"
收件人:
is_continuous_feed: false
如果这不起作用,我很抱歉。