I have an InlineDrawing in my document, which I can access as an element, and get it as "InlineDrawing".. I also know how to access an InlineImage and write it to a file as bytes. How would I export an InlineDrawing as an image of some kind, the formats available from the UI are svg, pdf, jpeg and png.
Google Apps script cannot do such image conversions for now... You could ask for an enhancement on the issue tracker
You could copy the paragraph into a new document. Then you can convert the new document to pdf. Something like this:
var temporaryFileName = Math.random().toString(36);
var doc = DocumentApp.create(temporaryFileName);
// Child is the element with the InlineDrawing.
var originalParagraph = child.getParent().copy();
doc.getBody().appendParagraph(originalParagraph);
doc.saveAndClose();
// Save image.
var blob = doc.getAs('application/pdf');
blob.setName("bla.pdf");
folder.createFile(blob);
Related
I'm trying to build a website using Google App Script where I copy all elements from my google form and show it in my own website. I was able to get the details of all elements using : https://developers.google.com/apps-script/reference/forms except for the Image elements.
This link doesn't have the information to get the image URL :
https://developers.google.com/apps-script/reference/forms/image-item
I was able to fetch the BLOB but I couldn't fetch the image URL.
For example in the screenshot, I need the highlighted URL of the image in my form : https://lh6.googleusercontent.com/uZtoCzoraW7vkkrN2289pX83Y6wwaRmMjpgBySWHaT3Vm2p9DVAA7Voy4CclYp2hve6c6zzzLkh-rF1yAX9fFPTTd940WMjwVtjcyMF2XCbdQ7YKQhhCfYxSUyKztcKacWCitDy4C31f9lQ
I need the URL of the image to add in the source tag of my website.
Is there a method in google app script that can fetch me the URL of an image/video.
Solution
After taking a further look to your question, I found out I could use the Blob onject you get from the form image to create a drive file which then can be used to get its webContentLink url to be used in the web app using the Drive API.
To be able to use the Drive API in your Apps Scrip script please, in your script editor go to Resources -> Advanced Cloud Services and enable the Drive API in the dialog that will pop up.
This is the piece of code I have used for achieving what you aimed for with self explanatory comments (this is a simplified web app that only focuses on achieving your purpose):
Code.gs file
// Apps SCript function to serve the HTML file in the web
function doGet() {
return HtmlService.createHtmlOutputFromFile('test');
}
// Function to be called in the HTML that returns the URL to be used by the image tag
function getUrl() {
var form = FormApp.getActiveForm();
// Get image blob
var image = form.getItems()[0].asImageItem().getImage().getAs('image/jpeg');
// Use the image blob for creating a new drive file
var file = DriveApp.createFile(image);
// Use the Drive API get method to get the property webContentLink which will return the link
// of the image to be used in a website
var fileURL = Drive.Files.get(file.getId()).webContentLink;
return fileURL;
}
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body id="body">
<h1>MY IMAGE</h1>
<script>
// Get what our function getURL returns
google.script.run.withSuccessHandler(function(URL) {
// Create HTML image tag
var img = document.createElement('img');
// Set its source to our file's sharable URL
img.src = URL;
// Attach image element to the body
document.getElementById('body').appendChild(img);
}).getURL();
</script>
</body>
</html>
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)
I don't have access to DriveApp but am trying to email a PDF of a newly created document with:
var doc = DocumentApp.create('Sample Document');
var body = doc.getBody();
// create lots of content in the document...
//
// after adding all the content and applying styles etc
// send the document as PDF to the user
var pdf_file = doc.getAs("application/pdf");
GmailApp.sendEmail('user#test.com', 'Attachment example', 'Please see the attached file.', {
attachments: [pdf_file],
name: 'Test Name'
});
The PDF attachment received, however, is a blank document and I think this is because the document is being "got" as a PDF file before the contents have been added to the document.
Is there any way for Google Apps Script to either:
Wait until the content has been added before converting to PDF?
Ensure that getAs() uses a "refreshed" version of doc (that has all the added contents)?
The PDF attachment received, however, is a blank document and I think this is because the document is being "got" as a PDF file before the contents have been added to the document.
Correct, add the line: doc.saveAndClose(); to the end of the code that builds the doc.
I need a simple way to export - saveAs or getAs, a google document to PNG or JPG and send it on email.
I was able to get it as pdf with 'application/pdf' with this: Class Blob
But wasn't able to use 'image/png' with the same code. The code I use to get .pdf is:
var pdf = DriveApp.getFileById("docID").getAs("application/pdf");
Should I change the code to function for PNG?
The documentation says:
For most blobs, 'application/pdf' is the only valid option. For images in BMP, GIF, JPEG, or PNG format, any of 'image/bmp', 'image/gif', 'image/jpeg', or 'image/png' are also valid.
A Google Document is not an image, so the only type offered is PDF.
What you can do is to save any images embedded in a document as PNG, like this.
var doc = DocumentApp.openByUrl('...');
var image = doc.getBody().getImages()[0]; // first image in document
var file = DriveApp.createFile(image.getBlob().setName('my image.png'));
So I've got a HTML file, that I am using to send emails, but in some instances I want it simply to use that file to create a PDF of the same template.
I've got it functioning for the most part - it creates the file, runs the evaluations and gets the content, but it doesn't actually render the html. It simply leaves all the html notation in place.
For example, it outputs a pdf but it reads:
Dear Martin, <br />
instead of:
Dear Martin
How do I make sure it renders the HTML so that the PDF is laid out correctly, and doesn't have the html code noted in the text?
Here's the code:
var docName = "test";
var htmlBody = HtmlService.createHtmlOutput(template.evaluate().getContent()).getContent()
var doc = DocumentApp.create(docName);
doc.appendParagraph(htmlBody);
doc.saveAndClose();
DocsList.createFile(doc.getAs('application/pdf')).rename(docName);
You can use your existing HTML as a blob, and convert it to PDF like this:
var htmlBody = HtmlService.createHtmlOutputFromFile('my_file_within_script_project.html').getContent();
var blob = Utilities.newBlob(htmlBody, 'text/html').getAs('application/pdf').setName('my_output_in_drive.pdf');
DriveApp.createFile(blob);
The best option when you want to create a PDF from a template is to use Google Docs directly so that no formatting is lost and also avoid the problem you are facing.
Why don't you just create your template directly in Google Docs. Have some placeholders such as {name} instead of the actual name. Instead of using template.evaluate(), you can do a find and replace in the doc.
Is there a way to export a page as a png with InDesign server?
The following code works for a text frame. How do I do the same for the complete page contents?
var theDocument = app.documents.add();
var thePage = theDocument.pages[0];
var theTextFrame = thePage.textFrames.add();
theTextFrame.geometricBounds = [5,5,40,40];
theTextFrame.contents = TextFrameContents.placeholderText;
theTextFrame.exportFile(ExportFormat.pngFormat, File("c:\\test.png"));
if you can export as JPG, something like this should work:
//set which page you want to export:
app.jpegExportPreferences.pageString='1';
//export that page from the document:
var myFile = new File('C:/test.jpg');
theDocument.exportFile(ExportFormat.JPG, myFile);
I'm not sure if setting the jpegExportPreferences.pageString would still work or not with exporting as a PNG, but you might be able to test that. Hopefully this at least gets you on the right track!
Note that if you want to export as a PNG, use this export format:
ExportFormat.PNG_Format
EDIT:
Looking at this article from the Adobe Forums, it states that InDesign can export as PNG but doesn't include any options, so it has limitations other than specifying the format. So, if the above code example won't help, maybe try something like this:
app.activeDocument.selection[0].exportFile(ExportFormat.PNG_FORMAT, File(new File("c:\\test.png")));
Hope this helps!