Using HTML file to output a PDF - google-apps-script

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.

Related

apps script getBody doesn't give full html contents

I get emails from a system that contains paragraphs and tables.
When I try to get full html format using getBody from email it gives me just CSS contents and not the whole html contents.
However when I copy full email body and send it to myself in a new email then getBody function gives me accurately full body in html format including all tags and contents.
Kindly guide what I am missing here?
var label = GmailApp.getUserLabelByName("INBOX/reports0");
var threads = label.getThreads();
var tempbody = threads[i].getMessages()[0].getBody();
Regards
As the logged output in the console is too large and it's being truncated you're not seeing the whole output in neither of the cases, you're getting the entire HTML you just don't see it there. You can use Stackdriver logging instead to show the whole output.

Google Script doc.getBody() returns plain text

Just a quick question, is there anyway that I can get the body of my google doc to be as exact as it is? For example, I have a bold sentence in my google doc, but whenever i'm trying to send it using via sendEmail in google script, it's being sent as plaint text, the bold letters an other formatted texts are being converted into plain, please see my code:
var body_hw1 = doc_hw1.getBody().getText();
MailApp.sendEmail(email, subject_hw1, body_hw1);
The method getBody() returns an object that is not usable outside of Google Docs. The method getText() returns plain text.
In order for use bold fonts, etc, in an email, it has to be formatted as HTML. So you need to convert Google document to HTML. There is no built-in function for this, but a third-party solution is available: it does not support full range of Docs format but certainly supports bold and italic fonts, and similar. See this answer.
Another solution would be to extract the plain text and format it into html yourself and send that. For example:
var myText = {header:'Dear sir,',message:'We are writing to inform you...'}
var message = ("<h3>"+ myText.header +"</h3>" +
"<p>"+ myText.message +"</p>")
MailApp.sendEmail(email, 'Important Message', message);

Sending HTML emails using google ap script

I'm trying to send HTML emails using Google aps script. I've got the HTML on a google doc which i'm trying to send using the code below. But when I send it I receive it as unformatted text with all the HTML tags displayed. Can anyone tell me how to do this? I'd rather not include the HTML in the script, because there will eventually be an awful lot of it.
var html = UrlFetchApp.fetch('https://docs.google.com/document/d/documentID/export?format=html');
MailApp.sendEmail('emailaddress', subject, null , {htmlBody: html});
Thanks, Bryan
Here's an example of doing the same thing with a row of spreadsheet data. Link. Don't use css between <style></style> tags. Use inline style="" instead. That seems to work better.
Google Script Mail App
The format of your code seems not well-structured,
Make sure all the data passed to the:
.sendEmail() method is in Curly braces {}.
Please reference the code below to see how it works.:
MailApp.sendEmail({
to: "me#myservice.com",
subject: "Testing",
htmlBody:"<h1>Hello there</h1><p>Your HTML Here</p>"});
That's it. You can also send this HTML from external file as:
HTMLService.createTemplateFromFile("fileName")

Retrieving HTML code used in google docs using google app scripts

Is it possible to use Google app scripts to get the html code in Google docs i.e if text in the document was say bold,i could get the html code for that.
What i have so far :
function getHtml(id) {
var doc1=DocsListExtended.getFileById("1ta7zJ6SDFgzgp-UprjehxR8Tx3-4-wtJwTYqWbol1SU").getAsHTML();
var body=HtmlService.createHtmlOutput(doc1);
var cont=body.getContent();
return cont;
Logger.log(cont);
}
I don't know a simple way for that. You can navigate the elements of the document using DocumentApp API, and converting it block by block to HTML.
I already tried
DocumentApp.getActiveDocument().getAs(MimeType.HTML).getDataAsString()
but the error says doc can't be converted to html mimetype.

How do I export InlineDrawing as an image in Document?

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);