HTML to PDF on click convert - html

I'm busy making a site and would like to know if there's a way to convert HTML to PDF or word file.
Scenario ~
User sees the information that he/she likes clicks on a button that converts the Page/Section to PDF or word and saves it locally (to the person's computer)
Any ideas? Anything would help if I could get some documentation or some direction it would be appreciated.
Thanks in advance.
Edit*
Sorry if it's a silly question still learning here :D

sure there are many ways, i can be helpful in JavaScript, i'm working on a project where client can download the Html files as PDF,please check
<script>
$(".clone-pdf").clone().appendTo(".html-content");
function CreatePDFfromHTML() {
var HTML_Width = $(".html-content").width();
var HTML_Height = $(".html-content").height();
var top_left_margin = 15;
var PDF_Width = HTML_Width + (top_left_margin * 2);
var PDF_Height = (PDF_Width * 1.5) + (top_left_margin * 2);
var canvas_image_width = HTML_Width;
var canvas_image_height = HTML_Height;
var totalPDFPages = Math.ceil(HTML_Height / PDF_Height) - 1;
html2canvas($(".html-content")[0]).then(function (canvas) {
var imgData = canvas.toDataURL("image/jpeg", 1.0);
var pdf = new jsPDF('p', 'pt', [PDF_Width, PDF_Height]);
pdf.addImage(imgData, 'JPG', top_left_margin, top_left_margin, canvas_image_width, canvas_image_height);
for (var i = 1; i <= totalPDFPages; i++) {
pdf.addPage(PDF_Width, PDF_Height);
pdf.addImage(imgData, 'JPG', top_left_margin, -(PDF_Height * i) + (top_left_margin * 4), canvas_image_width, canvas_image_height);
}
pdf.save("Your_PDF_Name.pdf");
});
}
</script>
and the html to send the request to the script.
<i class="icon-download"></i>
I wish i helps to get the answer you are looking for.

Related

Is there any way to see the names of a website's network requests with Google apps scripts?

So here's what I'm trying to do:
There's a website called Torah Anytime (https://www.torahanytime.com/) which publishes audio files (I guess you can call them podcasts, the website refers to them as shiurim, shiur being hebrew for song, or in this case, audio) on a daily basis. I would like to create a script that downloads the audio of specific speakers and then emails those files to me. The way I'm accomplishing this is with Google Apps Scripts. Torah Anytime allows you to follow specific speakers and to get email notifications when a speaker you're following puts out a new podcast. Here is the code that I have so far:
function main() {
var emails = getemails();
for (var i = 0; i < emails.length; i++) {
var email = emails[i].getMessages();
if (email[0].getFrom() == "TorahAnytime Following <following#torahanytime.com>"){
var title = getTitle(email);
var shiurID = getShiurID(email);
var downloadLink = "https://dl.torahanytime.com/audio/" + shiurID;
var shiur = downloadShiur(downloadLink);
shiur.setName(title);
var emailSent = emailShiur(shiur);
if (emailSent) {email[0].moveToTrash();
Logger.log("Email moved to Trash");}
}
}
}
function getemails() {
var label = GmailApp.getUserLabelByName("TA Speeches");
return label.getThreads();
}
function getTitle(email) {
body = email[0].getPlainBody();
var begIndex = body.indexOf("from") + 4;
var endIndex = body.indexOf("on ");
var title = body.substring(begIndex, endIndex).toLowerCase().replaceAll(" ", "-").replace(/(\r\n|\n|\r)/gm, "-");
begIndex = body.indexOf("called ") + 7;
endIndex = body.indexOf(" [");
title += "-" + body.substring(begIndex, endIndex).toLowerCase().replaceAll(" ", "-").replace(/(\r\n|\n|\r)/gm, "-") + ".mp3";
return title;
}
function getShiurID(email) {
body = email[0].getPlainBody();
var begIndex = body.indexOf("[") + 1;
var endIndex = body.indexOf("]");
var link = body.substring(begIndex, endIndex).replaceAll("?v", "?a");
console.log(link);
var mainLink = UrlFetchApp.fetch(link);
//here I somehow need to get the link being used to stream that particular audio file
}
function getIDName(email) {
body = email[0].getPlainBody();
var begIndex = body.indexOf("ID ") + 3;
var endIndex = body.indexOf(" and");
return body.substring(begIndex, endIndex);
}
function downloadShiur(downloadLink) {
var audio = UrlFetchApp.fetch(downloadLink);
return audio.getBlob().getAs('audio/mp3');
}
function emailShiur(shiur) {
const maxFileSize = 26214400;
if (shiur.getBytes().length <= maxFileSize) {
MailApp.sendEmail("[Email addressed removed]", "TA Shiur (File)", "Enjoy!", {
attachments: [shiur],
name: 'Automatic Emailer Script'
});
return true;
} else {
MailApp.sendEmail("[Email addressed removed]", "TA Shiur (File)", "Error, File too large to email", {
name: 'Automatic Emailer Script'
});
return false;
}
}
My issue is that the URL to download the file is not in the HTML, so I don't know how to get to it using GAS. If you use chrome's dev-tools, you can see the URL right there in the network tab Example of output I see that I want to get. Does anyone know of any way that I can get the information that I see in chrome's dev-tools network tab (the name's of the URLs being received) using GAS? Thank you!

How do you center text horizontally inside cells when inputting a template in docs?

Here's what I have:
function onOpen() {
DocumentApp.getUi()
.createMenu('New Table')
.addItem('Add Table', 'addTable')
.addToUi();
}
function addTable() {
var body = DocumentApp.getActiveDocument().getBody();
// body.insertHorizontalRule(1);
// var center = body.insertParagraph(1, ' ');
// center.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
var cells = [
['', 'Lunch','Dinner'],
['SALES', ' ',''],
['MEALS', ' ',''],
['IDEAL STAFFING LEVELS'],
['Senior','',''],
['Junior','',''],
['Host','',''],
['Kitchen','',''],
['Dish','',''],
['Service','',''],
['Expo','',''],
['Skip','',''],
['Super','',''],
[' '],
['Weather','',''],
['Events','',''],
['Other','',''],
['Day Manager:',''],
['Log:',''],
['Night Manager',''],
['Log',''],
];
var table1 = body.insertTable(2,cells);
table1.setColumnWidth(0, 125);
table1.setColumnWidth(1, 100);
table1.setColumnWidth(2, 100);
var style = {};
// style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
// DocumentApp.HorizontalAlignment.CENTER;
//style[DocumentApp.Attribute.FONT_FAMILY] = 'Calibri';
style[DocumentApp.Attribute.FONT_SIZE] = 7;
//style.table1.setVerticalAlignment(DocumentApp.VerticalAlignment.CENTER)
//style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
table1.setAttributes(style);
// var alignment = {};
// alignment[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
// DocumentApp.HorizontalAlignment.CENTER;
// //alignment.setAlignment(DocumentApp.HorizontalAlignment.CENTER);
// table1.setAttributes(alignment);
var tableHeader = table1.getRow(0);
var headerStyle = {};
//headerStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#C0C0C0';
headerStyle[DocumentApp.Attribute.BOLD] = true;
tableHeader.setAttributes(headerStyle);
var tableBold1 = table1.getRow(1);
var boldStyle = {};
boldStyle[DocumentApp.Attribute.BOLD] = true;
tableBold1.setAttributes(boldStyle);
var tableBold2 = table1.getRow(2);
var boldStyle = {};
boldStyle[DocumentApp.Attribute.BOLD] = true;
tableBold2.setAttributes(boldStyle);
var tableBold3 = table1.getRow(3);
var boldStyle = {};
boldStyle[DocumentApp.Attribute.BOLD] = true;
tableBold3.setAttributes(boldStyle);
}
Tried a variety of ways I have come across as seen in my notes but I am new to this and only starting to understand the smallest bits. I've read something about maybe needing to use insertParagraph here: How to insert and center text in Google Docs with script but am unable to figure out the correct format for that to work in this circumstance.
I would also like to be able to return the font size to normal after the table. What is the proper function for that?
Any help is greatly appreciated!
Thanks in advance
thisisnotadupp
This appears to be a bug!
I have taken the liberty of reporting this on Google's Issue Tracker for you, detailing the behaviour:
Horizontal and Vertical alignment not being respected programmatically in table cells in Google Docs
You can hit the ☆ next to the issue number in the top left on the page which lets Google know more people are encountering this and so it is more likely to be seen to faster.

find the length of a string in google script

I'm trying to make a script for google sheet, who can count a letter in a text. But it seems that .length doesn't work. Anyone who can give directions on where to find the the solution.
function Tjekkode(tekst , bogstav){
var test = "";
// find the length of laengdeTekst
var laengdeTekst = tekst.length;
var t = 0;
// through the text character by character
for ( var i = 1; i<laengdeTekst ; i++) {
var test = tekst.substr(i,1);
if (test == bogstav) {
// if the letter is found, it is counted up
// REMEMBER == means compare
var t = t + 1;
}
}
// returns percent appearance of the letter
Return = t / længdeTekst * 100
}
Thanks in advance
length is ok in your code. To test it, run this script:
function test( ) {
var test = "123456";
// finder længden på teksten
var laengdeTekst = test.length;
Logger.log(laengdeTekst);
}
After you run it, check Log, press [Ctrl + Enter]
The correct code in your case:
function Tjekkode(tekst, bogstav) {
var test = "";
var laengdeTekst = tekst.length;
var t = 0;
// start looping from zero!
for ( var i = 0; i<laengdeTekst; i++) {
var test = tekst.substr(i,1);
if (test == bogstav) {
var t = t + 1;
}
}
// JavaScript is case sensitive: 'return != Return'
return t / laengdeTekst * 100;
}
Please, look at this tutorial for more info
thanks
I'll guess that I might get the one with the R instead of r at the end, but the script didn't run that line, because it kinda stopped at the .length line :/
the comments in danish is for my pupils (I'm a teacher in elementary school)
I'll see if google wants to cooperate today :|
This is the google script that worked for me. Note the 24 - that's the length of an empty message that has markup like <div>...</div>
function TrashEmptyDrafts() {
var thread = GmailApp.getDraftMessages();
for (var i = 0; i < thread.length; i++) {
b=thread[i].getBody();
if (b.length <= 24.0){
thread[i].moveToTrash();
}
}}

Is there any tool to get data from website?

Let's say I have this link called: http://user.com/1345, and that link returns a JSON. Now I want a tool that can loop from http://user.com/1 to http://user.com/1345 and save that JSON data to a file. Anyone knows a good tool for that?
In JavaScript:
var allData = [], xhr;
for (var i = 1; i <= 1345; i++) {
xhr = new XMLHttpRequest();
xhr.addEventListener("load", function () {
allData.push(JSON.parse(this.responseText));
});
xhr.open("GET", "http://user.com/" + i);
xhr.send();
}
window.open('data:text/json;charset=utf8,' + encodeURIComponent(allData), '_blank');
You can use C# to do so.
for (var i = 0; i < 1345; i++)
System.IO.File.WriteAllText (i.ToString (), new WebClient.DownloadString ("http://user.com/" + (i + 1).ToString()));

Embed a video using Google Script

I'm trying to use Google Scripting to embed videos I have stored on my Google Drive into HTML. Here's the code I'm using to generate the HTML:
function GenerateTables() {
var folderz = DocsList.getFolder('SharedVideos');
var contents = folderz.getFiles().sort(function(a,b) {return b.getDateCreated()-a.getDateCreated()});
var file;
var name;
var date;
var url;
var dateModified;
var folder;
var textOutput = "<table>";
textOutput += "<tr><th>File Name</th><th>Create Date</th><th>Modified Date</th></tr>"
for (var i = 0; i < contents.length; i++) {
file = contents[i];
folder = file.getParents()[0];
name = file.getName();
url = "https://docs.google.com/file/d/" +file.getId()+"/preview";
date = Utilities.formatDate(file.getDateCreated(), 'GMT-6', 'MM/dd/yyyy hh:mm:ss aaa');
dateModified = Utilities.formatDate(file.getLastUpdated(), 'GMT-6', 'MM/dd/yyyy hh:mm:ss aaa');
textOutput += "<tr><td>" +name + "</td><td>" + date + "</td><td>" + dateModified + "</td></tr>";
textOutput += "<tr><td colspan='3'>";
textOutput += "<iframe src='"+url+"' height='385' width='640'></iframe>";
textOutput += "</td></tr>";
}
textOutput +="</table>";
return textOutput;
}
function doGet() {
var text = GenerateTables();
return HtmlService.createHtmlOutput(text);
}
The iframe is being converted to
<iframe data-caja-src='[the correct URL]' height="385" width="640"></iframe>
I was hoping it would be a simple process, but the more I got looking into it, the more complicated it seemed to be. Is there an easier way to do what I'm trying to do? I don't have much experience using Caja, but maybe there's a simple way to embed a video using Caja, I just couldn't get anything to work.
There is not yet a way to embed videos inside HtmlService. I think there's a feature request for it on the issue tracker, but regardless it's something we know about.