Is it possible to replace text within a pdf file using Google Apps Script?
I am trying the following code without success on the replace, it seems like the string is encoded in a way I cannot understand.
var pdfFile = DocsList.getFileById("pdf-doc-id");
var asBlob = pdfFile.getBlob();
var asString = asBlob.getDataAsString();
var s2s = "old string";
var s2r = "new string";
var repString = asString.replace(s2s, s2r);
var repBlob = Utilities.newBlob(repString).setContentType("application/pdf").setName("Testing");
DocsList.createFile(repBlob);
EDIT1: I got an empty pdf back
Any ideas?
Thanks
The function
getDataAsString()
doesn't return the textual content of a PDF file, but instead a textual representation of the binary content of the file. That function works on any file, even those that don't have text (like images).Unfortunately I don't think you can fully accomplish your goal with Apps Script. If you are able to import your PDF as a Google Document using the Drive UI, then you can use Apps Script's DocumentApp to modify the document and export it as a PDF.