window.onload = function () { generate(); addEvent( document.getElementById('generate'), 'click', function () { generate(); } ); addEvent( document.getElementById('download'), 'click', function () { createPDF(); } ); }; function addEvent(element, evnt, funct){ if (element.attachEvent) return element.attachEvent('on'+evnt, funct); else return element.addEventListener(evnt, funct, false); } let statsData = {}; function generate() { fetch("https://character.turtlebard.com/") .then(function (response) { // The API call was successful! return response.json(); }) .then(function (data) { statsData = data; document.getElementById("stats").innerHTML = '' + statsData.summary + " " + "" + "" + "" + "STR" + "" + statsData.strength + "" + "NAME" + "" + statsData.name + "" + "" + "" + "DEX" + "" + statsData.dexterity + "" + "RACE" + "" + statsData.race + "" + "" + "" + "CON" + "" + statsData.constitution + "" + "CLASS" + "" + statsData.class + "" + "" + "" + "WIS" + "" + statsData.wisdom + "" + "AGE" + "" + statsData.age + "" + "" + "" + "INT" + "" + statsData.intelligence + "" + "" + "" + "" + "" + "CHA" + "" + statsData.charisma + "" + "" + "" + "" + "" + ""; }) .catch(function (err) { console.warn("Something went wrong.", err); }); } window.jsPDF = window.jspdf.jsPDF; function createPDF() { let pageWidth = 8.5, filename = "" + statsData.name + " the " + statsData.race + " " + statsData.class + " :: Generated by TurtleBard_com.pdf"; lineHeight = 1.2, margin = 1, maxLineWidth = pageWidth - margin * 2, fontSize = 14, ptsPerInch = 72, oneLineHeight = (fontSize * lineHeight) / ptsPerInch, text = ""+ statsData.summary + "\n" + "\n"+ "Name: " + statsData.name + "\n" + "Race: " + statsData.race + "\n" + "Class: " + statsData.class + "\n" + "Age: " + statsData.age + "\n\n" + "Strength: " + statsData.strength + "\n" + "Dexterity: " + statsData.dexterity + "\n" + "Constitution: " + statsData.constitution + "\n" + "Wisdom: " + statsData.wisdom + "\n" + "Intelligence: " + statsData.intelligence + "\n" + "Charisma: " + statsData.charisma + "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "Generated using the 5e NPC Tool at TurtleBard.com/5eNPC", doc = new jsPDF({ unit: "in", format: "letter", lineHeight: lineHeight }).setProperties({ title: "" + statsData.name + " the " + statsData.race + " " + statsData.class +" (A Turtle Bard 5e NPC)", author: "Turtle Bard", creator: "Turtle Bard" }); // splitTextToSize takes your string and turns it in to an array of strings, // each of which can be displayed within the specified maxLineWidth. let textLines = doc .setFont("helvetica") .setFontSize(fontSize) .splitTextToSize(text, maxLineWidth); // doc.text can now add those lines easily; otherwise, it would have run text off the screen! doc.text(textLines, margin, margin + 2 * oneLineHeight); doc.save(filename); } Generate Download PDF