I am creating PDF files using blobs in Google Apps Script from a HTML code, but the problem is that HTML code has an image (referenced by "http") but the created pdf can't show it.
This is my code
function createBlobPDF(myMessage,myTitle){
var blobHTML = Utilities.newBlob(myMessage, "text/html", myTitle+ ".html");
var myPDF = blobHTML.getAs("application/pdf");
return myPDF;
}
Any solution? thanks!!
If the images in HTML are loaded from URLs, the converted PDF file doesn't include the images. If you want to include the images in the converted PDF file, please put the images to HTML as base64 data.
In order to confirm this situation, please see the following sample script. This sample script puts an image as URL and base64 data. The result is displayed them to a dialog and created a PDF file.
When you use this script, please copy and paste to the script editor of Spreadsheet.
Sample script :
function myFunction() {
// image
var url = "https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a";
var blob = UrlFetchApp.fetch(url).getBlob();
var b64 = blob.getContentType() + ';base64,'+ Utilities.base64Encode(blob.getBytes());
var html = "URL<img src=\"" + url + "\">Base64<img src=\"data:" + b64 + "\">";
var h = HtmlService.createHtmlOutput(html);
// Open dialog with the images.
SpreadsheetApp.getUi().showModalDialog(h.setWidth(500).setHeight(200), 'Sample');
// Create from HTML to PDF file.
DriveApp.createFile(h.getAs("application/pdf").setName("text.pdf"));
}
Result of dialog :
Both images can be seen.
Result of PDF file :
Only the image of base64 can be seen.
Note :
This is a simple script. So please modify this to your environment.
References :
How to display Base64 images in HTML?
If I misunderstand your question, I'm sorry.
I tested converting html to pdf with image content. It works but in the bottom of a box, the color does not work! How to make the color to appear in the background of a box?
I wanted to show you my little treatment as an example "<>" but it doesn't work because there is no window for code.gs . Shame...
In short, see the final result in pdf, attached
before clicking on the "validate" button, the screen is displayed, see the screenshot below. this is where you "capture" the HTML presentation and then run to convert HTML to PDF.
after clicking on "validate", here is the result, see below :
normally, I would like to have the same result between screen and PDF.
Then, how to do it ?
Related
I want to get text from an image placed in a cell using Google Apps Script.
What code do I write in Google Apps Script?enter image description here
Can the cell be saved as an image and the text read?
I believe your goal is as follows.
You have a Google Spreadsheet where the images are put into the cells. In this case, the images are directly put into the cells without URLs and the built-in function. The images show some texts.
You want to retrieve the images and retrieve the texts from the images.
Unfortunately, in the current stage, there are no built-in methods for directly retrieving the images in the cells. So, in this case, I use this workaround for retrieving the images from the cells. And, in order to convert the texts from the image, Drive API is used.
When these are reflected in a sample script, it becomes as follows.
Usage:
1. Install a Google Apps Script library.
In this workaround, DocsServiceApp (Author: me) of a Google Apps Script library is used. Please install this library. You can see how to install it at here.
2. Sample script.
Please copy and paste the following script to the script editor of Spreadsheet. And, please set your sheet name.
function myFunction() {
const sheetName = "Sheet1"; // Please set your sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const spreadsheetId = ss.getId();
const ar = DocsServiceApp.openBySpreadsheetId(spreadsheetId).getSheetByName(sheetName).getImages();
const res = ar.map(({ range, image }) => {
const tempId = Drive.Files.insert({ mimeType: MimeType.GOOGLE_DOCS, title: "temp" }, image.blob).id;
const text = DocumentApp.openById(tempId).getBody().getText();
Drive.Files.remove(tempId);
return { cell: range.a1Notation, text: text.trim() };
});
console.log(res);
}
In this script, the images in the cells are retrieved using a library, and the images are converted to Google Documents. And, the text is retrieved from Google Documents.
3. Enable Drive API.
Please enable Drive API at Advanced Google services. Ref
4. Testing.
When the above script is run for your provided Spreadsheet, the following result can be seen in the log.
[
{"cell":"A1","text":"098765431 ZYX"},
{"cell":"A2","text":"1234567890 abcdef"},
{"cell":"A3","text":"098765431 ZYX"},
{"cell":"A4","text":"1234567890 abcdef"},
{"cell":"A5","text":"1234567890 abcdef"},
{"cell":"A6","text":"098765431 ZYX"},
{"cell":"A7","text":"098765431 ZYX"}
]
Note:
In this answer, I propose a simple method. When your sample Spreadsheet is used, the processing time was about 30 seconds. If your actual Spreadsheet has a lot of images, it might be required to split the cells and use the script. Please be careful about this.
References:
DocsServiceApp (Author: me)
Files: insert
Files: delete
How I can get the image that the user just uploaded to display in another element on the same page? I'm using the storeAsFile property within the filePond options.
As note, I'm currently displaying the image with ImagePreview plugin within the filepond input instance, but i want to show this image on another part of the page. Hope i'm being clear enough.
Found the solution,
pondPhoto.on('addfile', (error, file) => {
// this object contains the file info
console.log(file.file)
// you can construct a blob object
const imageSrc = URL.createObjectURL(file.file)
// then you may attach it to any image in your DOM
document.querySelector('img').setAttribute('src', imageSrc)
})
I'm working on a project that uses express.js for backend and the ejs rendering template for frontend. I've uploaded some images using filepond and the images were converted to base64. However, while viewing the output on the browser, the images look as though they're broken (with a small square at the top-left corner).
I need help with getting this fixed. Here's the code for the function to save the images and convert to base64:
function saveCover(book, coverEncoded) {
if (coverEncoded == null) return;
const cover = coverEncoded;
if (cover != null && imageMimeTypes.includes(cover.type)) {
book.coverImage = new Buffer.from(cover.data, "base64");
book.coverImageType = cover.type;
}
}
The problem I believe will be coming from how you are retrieving the image. If the code you've shown above saves the image (i.e you are seeing some data in the database), then you should focus your attention on the code that retrieves the image to display on the webpage.
I am trying to save a link in DotNetNuke (DNN) using the CKEditor for the HTML module.
When I save, the editor will automatically adjust the link.
I am trying to save it as
data-src="#bronze"
The reason for the hashtag is for displaying a fancybox pop-up with hidden content. https://fancyapps.com/fancybox/3/docs/#inline
But the editor adds /portals/2/ in front of this URL.
I have looked at this article below.
CKEditor - Change image source
I assume the CKEditor is saving the SRC and Href links in protected mode for browsers. Is there a way that I can turn this off in the settings?
I did try to change to RAW mode, but it still does the same thing.
I face the same problem on data-fancybox-href, the only solution I can think of is using jQuery / javascript to change back to correct value:
var src = $(".more_info_btn")
$.each(src, function(){
var href = src.attr("data-src")
var hrefArr = href.split("#")
href = "#" + hrefArr[hrefArr.length-1]
$(this).attr("data-src", href)
})
Is it possible to do something like this?
$.post( '/generate/file', json_data_striginfied, function( data ) {
var win = window.open()
window.write(data)
});
where data returned is binary data (a PDF file)?
I've tried it and the new window contains the binary data as text, it doesn't force a download. I would like it to force a download of the PDF file.
Try to add the remote PDF url to the new window source url.