I'm fairly new so forgive my question.
I am trying to overwrite a pdf file with a script, but would like to retain the same URL so that users can click the URL and get a dynamically changing pdf (rosters being filled).
setContent(content) only allows me to write a string to a file, not replace the pdf.
You can use the Advanced Drive API to overwrite the content of a file without changing the URL.
var file = DriveApp.getFileById("FILEID");
Drive.Files.update({
title: file.getName(), mimeType: file.getMimeType()
}, file.getId(), blob);
Related
I have stored a pdf file to appDataFolder which is application specific, using the get call I am trying to get the file url using 'webViewLink, webContentLink'. But opening the links does not show anything but using alt=media I was able to get the buffer and write it to a file.
const test = await drive.files.get({
fileId: fileId,
fields: 'webViewLink, webContentLink',
})
I want to download and rename a PDF file that is generated "on the fly" by an Angular app.
With this code, the PDF file is downloaded but I want to rename it before writing to disk.
//Download PDF
await secondTab.waitForSelector('img[title="Format PDF"]', { visible: true, timeout : 10000 })
await secondTab.waitForTimeout(10000);
var current_element = await secondTab.$('img[title="Format PDF"]');
var parent_node = await current_element.getProperty('parentNode');
await parent_node.click();
console.log("Downloading PDF");
I wasn't able to find a solution. All solutions talk about files downloaded from URL.
I tried to trace events on the page (cf. PageEventObjet), but no one catch this "on the fly" generated PDF file.
My question : is there a way to catch that stream, name it and save it to disk ?
Thank you very much.
Note : I dont want to play with the File system (i.e. find my downloaded PDF, rename it, move it, etc.)
The files are saved in a Drive folder. I need to send the text content of all .docx file as an API payload. I've tried using Blob but to no avail. Is there a way to get this done?
If I understand you correctly, you want to send the text content of a docx file that you have in Drive. If that's correct, then you can do the following:
function docx() {
var docxId ="your-docx-id";
var docx = DriveApp.getFileById(docxId);
var blob = docx.getBlob();
var file = Drive.Files.insert({}, blob, {convert:true});
var id = file["id"];
var doc = DocumentApp.openById(id);
var text = doc.getBody().getText();
return text;
}
This code uses Advanced Drive Service to create a Docs file out of the blob you get from the docx, via Drive.Files.insert. Then, you can easily access this newly created file via DocumentApp and use getText.
Bear in mind that this will create a new file every time you run it. Use Files.delete to avoid that.
I hope this is of any help.
For some reason, when converting an HTML file to a PDF in Google Apps Script, the file size of the PDF ends up being way bigger than the original HTML file.
The code I used to run the test is below:
function convertHTMLtoPDF() {
// convert a specific message with an attached HTML file to PDF,
// and save it to the root directory of Google Drive
var threads = GmailApp.search('subject:(Sample HTML)');
var message = threads[0].getMessages()[0];
var attachmentName = message.getAttachments()[0].getName();
var attachment = message.getAttachments()[0].getAs("application/pdf");
DriveApp.createFile(attachment).setName(attachmentName);
}
NOTE: You just need to take the sample HTML file I'm sharing below, attach it to an email with the subject: Sample HTML, and send it to yourself to allow the script to find it.
The sample HTML file I was converting is 133 KB. The converted is 10 MB after the script is run.
My test results are as follows:
Converting the sample.html file with Google Apps Script causes the 133 KB HTML file to become a 10 MB PDF.
Converting the sample.html file with Adobe Acrobat XI Pro causes the 133 KB HTML file to become a 151 KB PDF.
Does anyone know why the file size of the PDF that is converted through Google Apps Script is so much bigger than when converting with Adobe? Is there a way to reduce the file size?
Thanks in advance for any help with this!
I tried your code and I confirmed that the size became larger(10 MB). I think this is natural because PDF file is always large compared to the original type of the file. You can find many reasons in the internet that can explain that. One example is this.
To confirm that, I used this script to confirm that size.
function htmlToPDF() {
var html = "YOUR HTML CODE";
var blob = Utilities.newBlob(html, "text/html", "text.html");
var pdf = blob.getAs("application/pdf");
DriveApp.createFile(pdf).setName("text.pdf");
MailApp.sendEmail("sample#email.org", "PDF File", "",
{htmlBody: html, attachments: pdf});
}
And I also get 10MB size from this code.
Im sure you know the option, that, if you click on a link of a GOogle Doc, you can adjust the link with
/copy, and each time you open the document, you have the option to click on "Make a copy" and
a copy of the original file (Doc, sheet...) gets created and saved on "My drive" per default.
My question now is, can I somehow change this default path, where the files gets saved?
Like, can i say, save it per default in Google Drive folder xy, or can I even say, that
he shall ask in which of my Drive folder, the copy shall get saved?
Thanks for any hint
Try using makeCopy(name, destination).
makeCopy(name, destination)
Creates a copy of the file in the destination directory and names it with the name provided.
function myFunction() {
var files = DriveApp.getFilesByName('Sample Document');
while (files.hasNext()) {
var file = files.next();
var copyFolder = DriveApp.getFolderById('FOLDERID')
file.makeCopy('Sample Document Copy',copyFolder)
}
}
Note:
The folder must be selected using folderID not byName( based on experience and the tutorial example)
Valuable Sources:
Developer Guide
tutorial - How to use the makeCopy(name, destination) function (This is very useful!)
Hope it helps.