Replaced text in a Doc is missing in converted PDF

末鹿安然 提交于 2019-12-01 12:40:30

问题


I am starting in Google Apps Script and trying to create a copy of a file (Google Doc), then replace text in the copied document and convert it to PDF.

The function works fine, but the text replaced does not appear in the PDF file, but appears in the copied file (where I replaced).

I read about the saveandclose() method to save changes and close the document (copied) before the function finishes to eject. But it seem that the method is not available. I really appreciate the help. Thanks.

function replaceconvert() {
  var cdoc = DriveApp.getFileById('iddocument').makeCopy('filename321');
  var iddoc = cdoc.getId();

  var doc = DocumentApp.openById(iddoc);
  doc.getBody().replaceText('nombre', 'Juan Perez');

  var doc_repl = DriveApp.getFileById(iddoc);
  var blob = doc_repl.getAs(MimeType.PDF);

  DriveApp.getFolderById(idfolder).createFile(doc1)
}

回答1:


I do use the following function, which works for me:

function personaliseAttachment(keyTemplate, member, fileName){

  var cloneId = DriveApp.getFileById(keyTemplate).makeCopy('cloneAttachment').getId();
  var clone = DocumentApp.openById(cloneId);

  var body = clone.getBody();

  for (var property in member) {
    if (member.hasOwnProperty(property)) {
      body.replaceText("{{"+property+"}}", member[property]);
    }
  }
  clone.saveAndClose();

  var clonePDF = DriveApp.createFile(clone.getAs('application/pdf'));
  clonePDF.setName(fileName);

  DriveApp.getFileById(cloneId).setTrashed(true);

  return clonePDF;
}


来源:https://stackoverflow.com/questions/48236363/replaced-text-in-a-doc-is-missing-in-converted-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!