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'));
Related
Some PDF files won't render in Chrome browser but will render fine in Firefox. All files render fine in all browsers if emeded directly.
<object id="content-view" :data="content_view.base64" type="application/pdf"></object>
The confusing part is that the problem is only for some files and not all. Files are stored in a folder that is not public and that's why they are served as base64 for the user to view.
I tested the problematic files by using online base64 decoders and I get the same result. Rendered in FF, not rendered in Chrome.
I cannot share any of the PDF files. They are all from the same source, scanned from the same device, PDF version 1.4, 4 pages.
I have tried:
using iframe, embed and object (same result)
unblocking Insecure content in Chrome site settings
opening and re-saving in Adobe Acrobat
using online PDF analyzers to see if any problems present (none found)
I had the exact same issue. I noticed that some of the PDFs that are more than 1MB isn't loading.
I found a solution here: Open base64 encoded pdf file using javascript. Issue with file size larger than 2 MB
Need to change the Base64 string to a BLOB. Then create a URL to be used for iframe src.
Here is the code:
base64PDFToBlobUrl( base64 ) {
const binStr = atob( base64 );
const len = binStr.length;
const arr = new Uint8Array(len);
for (let i = 0; i < len; i++) {
arr[ i ] = binStr.charCodeAt( i );
}
const blob = new Blob( [ arr ], { type: 'application/pdf' } );
const url = URL.createObjectURL( blob );
return url;
}
This will return a url that you can put inside your iframe, embed or object src. This way, you can still load the PDF in a page without opening it in another tab.
Rather than use the browsers native PDF renderer, you could use the JS one written by Mozilla.
ViewerJS provides a nice interface to this and if you want to embed it fullsize in a page, then you can place it in an iframe and control that with iFrame-resizer.
Instead of opening the PDF file in HTML object element, open it in a new window using Blob URL.
Please refer
Creating a BLOB from a Base64 string in JavaScript
to convert Base64 to Blob
After downloading an image from s3, I get a bunch of bits inside the 'Body'. Previously, I was encoding such bits into base64 and displaying them directly in a <img> tag.
But now, I want to save them into a "real" file (.jpg/png) in the filesystem (android/iOS) then give the path to a html (background) tag.
(will it work?)
Do I still need to convert the bunch of bits I got from S3 into base64 then save it or is there a more straightforward way ?
You can use the plugin cordova-plugin-file for saving file locally to your filesystem.
Like this
var url = url;
var targetPath = cordova.file.externalDataDirectory + NAME;
//alert(targetPath);
var trustHosts = true;
var options = {};
$cordovaFileTransfer.download(url, targetPath, options, trustHosts)
I think you can give the local file address to img src..
I am downloading an image when a div is clicked by using...
document.location.href = save_canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
This is working but the image that downloads has no extension and is just called 'download'
I have tried setting the name like this...
document.location.download = "myfile.jpg";
document.location.href = save_canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
But it is having no effect, where am i going wrong?
The download attribute is not part of the Location object as document.location holds, only for the HTML anchor (A) tags (except in IE).
Depending on browser and version you could instead try to convert canvas into a Blob object, then to File in order to set a filename, and serve that as URL via URL.createObjectURL(). Also here, toBlob() is not supported in IE (but you can polyfill toBlob(), and use msSaveBlob instead).
(and you would also want to replace mime-type's "image" with "application" for mime-type (e.g. "application/octet-stream"). )
c.toBlob(function(blob) {
var file = new File([blob], "test.png", {type: "application/octet-stream"});
document.location.href = URL.createObjectURL(file);
})
A save request with PNG and filename should appear when running this code...
<canvas id=c></canvas>
Optionally, try the FileSaver.js library which deals with many special cases.
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);
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!