I am trying to generate pdf from HTML table using jspdf.In this case the pdf is generated but the format is not suitable to original.
This is my code.
html code is
<div class="invoice" id="customers">
<table ng-repeat="aim in input" id="example">
<tr>
<th class="inv-left"><div align="left"><img src="./images/logo.png" alt=""></div></th>
<th class="inv-right"><div align="right"><br>
101 Convention Center<br>
dr #700, Las Vegas, <br>
NV - 89019
</div></th>
</tr>
<tr >
<th><div cg-busy="{promise:viewPromise}" align="left">
<b>Invoiced to</b><br>
{{aim.user.username}}<br>
{{aim.vendor.address}}
</div></th>
<th class="inv-right">
<div align="right"><b>INVOICE</b><br>
Invoice ID: {{aim.invoiceId}}<br>
Invoice Date: {{aim.invoiceDate.date| dateFormat | date:'MM-dd-yyyy'}}<br>
Due Date: {{aim.dueDate.date| dateFormat | date:'MM-dd-yyyy'}}
</div></th>
</tr>
<div class="invoice-content clearfix" cg-busy="{promise:viewPromise}" >
<tr>
<td class="inv-thours">Total Hours</td>
<td align="center">{{aim.totalHours}}</td>
</tr>
<tr>
<td class="inv-rate">Rate</td>
<td align="center">{{aim.billRate}}</td>
</tr>
<tr>
<td class="inv-rate">Amount</td>
<td align="center">{{(aim.totalHours) * (aim.billRate)}}</td>
</tr>
<tr>
<td class="inv-thours">totalExpenses</td>
<td align="center">{{aim.totalExpenses}}</td>
</tr>
<tr>
<td class="inv-thours">Total Amount</td>
<td align="center">{{aim.amount}}</td>
</tr>
<tr>
<td>
</td>
<td ng-if="aim.status === 'UNCONFIRMED'">
<div align="right" style="margin-right:10px;"><input type="submit" value="Confirm" data-ng-click="confirmStatus(aim)"> |
<button onclick="goBack()">Cancel</button></div>
</td>
<td ng-if="aim.status === 'CONFIRMED'">
<div align="right" style="margin-right:10px;">
<button onclick="goBack()">BACK</button></div>
</td>
<td ng-if="!(aim.status === 'UNCONFIRMED') && !(aim.status === 'CONFIRMED')">
<button onclick="javascript:demoFromHTML();">PDF</button>
</td>
</tr>
</table>
<script type="text/javascript" src="http://mrrio.github.io/jsPDF/dist/jspdf.debug.js"></script>
<script>
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter');
var imgData = '.............';
pdf.setFontSize(40);
pdf.addImage(imgData, 'PNG', 12, 30, 130, 40);
pdf.cellInitialize();
pdf.setFontSize(10);
$.each($('#customers tr'), function (i, row) {
$.each($(row).find("th"), function (j, cell) {
var txt = $(cell).text();
var width = (j == 4) ? 300 : 300; //make with column smaller
pdf.cell(10, 30, width, 70, txt, i);
});
$.each($(row).find("td"), function (j, cell) {
var txt = $(cell).text().trim() || " ";
var width = (j == 4) ? 200 : 300; //make with column smaller
pdf.cell(10, 50, width, 30, txt, i);
});
});
pdf.save('sample-file.pdf');
}
I whant to generate pdf to this formate
http://i.stack.imgur.com/nrR7l.png
but generate pdf formate is
http://i.stack.imgur.com/DGSxE.png
please help me to this problem.
Thank you.
I think CSS is missing in your generated PDF, and found this,
github issue link
diegocr commented on 25 Sep 2014
I'm afraid the fromHTML plugin is kinda limited when it comes to support css styles. Also, we have an addSVG plugin to deal with SVG elements, but the fromHTML does not uses it. So, no, the issue isn't Angular, you may could use the new addHTML (#270) but i dunno if that will deal with SVG. (html2canvas, that is)
Related
is it possible to display the data on which column month of the table?
below is the sample dashboard to achieve
and this is my DB
currently this is my dashboard. I want to align the data base on the period in the DB.
what should be my tboby look like to achieve the desired dashboard
this is my index html
<div class="w3-container w3-padding-64 w3-theme-l5">
<div class="w3-padding-16"><span class="w3-xlarge w3-border-purple w3-bottombar">COUNT PER BUSINESS UNIT</span></div>
<div>
<table id="businessUnit-tbl" class="table table-bordered display nowrap">
<thead>
<tr style="/*color:white;*/ align-content:center">
<td>MONTH</td>
</tr>
<tr #*style="color:white"*#>
<td>Business Unit</td>
<td id="jan">January</td>
<td id="feb">February</td>
<td id="mar">March</td>
<td id="apr">April</td>
<td id="may">May</td>
<td id="june">June</td>
<td id="july">July</td>
<td id="aug">August</td>
<td id="sept">September</td>
<td id="oct">October</td>
<td id="nov">November</td>
<td id="dec">December</td>
<td id="total">Total</td>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
According to my understanding of the topic, I suggest leaving tbody blank and trying to fill it with javascript
// append column to the HTML table
function appendColumn() {
var tbl = document.getElementById('my-table'), // table reference
i;
// open loop for each row and append cell
for (i = 0; i < tbl.rows.length; i++) {
createCell(tbl.rows[i].insertCell(tbl.rows[i].cells.length), i, 'col');
}
}
I have an http POST response which I receive in HTML. Now I want to display the results in my view Controller. How can I parse the DOM of the response to get the elements I want?
This is the response in raw html:
<tr>
<td style="text-align:center;">1</td>
<td>9.99</td>
<td style="text-align:center;" class="show_on_masters hide"></td>
<td style="text-align:center;">1.4</td>
<td>DE GRASSE, ANDRE</td>
<td style="text-align:center;">ON</td>
<td>
<div data-tooltip="Speed Academy Athletics Club">SAAC</div>
</td>
<td>94</td>
<td>2</td>
<!--<td class="rankings_hide_992">UF Tom Jones Invitational (Olympic Development)</td>-->
<!--<td class="rankings_hide_768">Gainesville , FL</td>-->
<td>
<div data-tooltip="UF Tom Jones Invitational (Olympic Development)" style="cursor:default;">Gainesville , FL</div>
</td>
<td>17/04/2021</td>
</tr>
<tr>
<td style="text-align:center;">2</td>
<td>10.08</td>
<td style="text-align:center;" class="show_on_masters hide"></td>
<td style="text-align:center;">1.9</td>
<td>BROWN, AARON</td>
<td style="text-align:center;">ON</td>
<td>
<div data-tooltip="Phoenix Athletics Assoc. of Ontario">PHNX</div>
</td>
<td>92</td>
<td>7</td>
<!--<td class="rankings_hide_992">World Athletics - Miramar</td>-->
<!--<td class="rankings_hide_768">Miramar, FL</td>-->
<td>
<div data-tooltip="World Athletics - Miramar" style="cursor:default;">Miramar, FL</div>
</td>
<td>10/04/2021</td>
</tr>
<tr>
<td style="text-align:center;">3</td>
<td>10.14</td>
<td style="text-align:center;" class="show_on_masters hide"></td>
<td style="text-align:center;">0.7</td>
<td>WARNER, DAMIAN</td>
<td style="text-align:center;">ON</td>
<td>
<div data-tooltip="London Western T.F.C.">LWTF</div>
</td>
<td>89</td>
<td>1dec5</td>
<!--<td class="rankings_hide_992">Hypo-Meeting</td>-->
<!--<td class="rankings_hide_768">Götzis, AUT</td>-->
<td>
<div data-tooltip="Hypo-Meeting" style="cursor:default;">Götzis, AUT</div>
</td>
<td>29/05/2021</td>
</tr>
I'm currently trying to use HTMLKit based on a couple tutorials, but I can't truly traverse the DOM with this library. Any ideas?
HTMLKit Tutorial
HTMLKit Video Tutorial
You can try SwiftSoup library that allows HTML parsing.
Usage
do {
let html: String = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
let doc: Document = try SwiftSoup.parse(html)
let link: Element = try doc.select("a").first()!
let text: String = try doc.body()!.text(); // "An example link"
let linkHref: String = try link.attr("href"); // "http://example.com/"
let linkText: String = try link.text(); // "example""
let linkOuterH: String = try link.outerHtml(); // "<b>example</b>"
let linkInnerH: String = try link.html(); // "<b>example</b>"
} catch Exception.Error(let type, let message) {
print(message)
} catch {
print("error")
}
I have a file that comes from the bank in an HTML format. They send this file as a transaction happens and I have a appscript to save the attachment to a Drive folder. I want to parse the HTML and copy the table to a spreadsheet appending it at the bottom.
I have created this piece of code, but XML parsing of HTML is a mystery for me, so It doens't work
function parseTablesFromHTML() {
var folderId = "1NrgsTgB3q573wav3cQsse4sAT8poeI77";
var folder = DriveApp.getFolderById(folderId);
var htmls = folder.getFilesByType(MimeType.HTML);
var sheetId = "https://docs.google.com/spreadsheets/d/1B-mjEUqvy49Wvct13XrWi6TU1dw1VPwesfYJRKJ5T6s/edit#gid=0";
//Set up spreadsheet
var ss = SpreadsheetApp.openByUrl(sheetId);
SpreadsheetApp.setActiveSpreadsheet(ss);
Logger.log('File name: ' + ss.getName());
var sheet = ss.getSheetByName("test");
var range = sheet.getRange(sheet.getLastRow(),sheet.getLastColumn());
while (htmls.hasNext()) {
var html = htmls.next().getId();
var response = DriveApp.getFileById(html).getBlob().getDataAsString();
var xmlDoc = XmlService.parse(response);
var b = xmlDoc.getElement().getElement("body");
var table = b.getElement("div").getElement("div").getElement("div").getElements("div")[1].getElement("table");
var rows = [];
var trs = table.getElements("tr");
for (var r=0,rlength=trs.length; r<rlength; r++) {
var tds = trs[r].getElements("td");
var row = [];
for (var c=0,clength=tds.length; c<clength; c++) {
row.push(tds[c].getText());
}
rows.push(row);
}
Logger.log(Utilities.jsonStringify(rows));
}
}
the html code looks like this
<table bordercolor="black" cellspacing="0" cellpadding="4" width="100%" class="style0" BORDER="1" FRAME="BOX" RULES="NONE">
<tr>
<td class="td-header" align="center" width="15%">Process date</td>
<td class="td-header" align="center" width="5%">Reference</td>
<td class="td-header" align="center" width="10%">Value date</td>
<td class="td-header" align="right" width="5%">Amount</td>
<td class="td-header" align="center" width="5%">Type</td>
<td class="td-header" align="left" width="40%">Description</td>
<td class="td-header" align="center" width="25%">details BISERA</td>
</tr>
<tr>
<td colspan="7">
<hr size="1" />
</td>
</tr>
<tr>
<td nowrap="nowrap" align="center">02.10.2020 16:13:22</td>
<td nowrap="nowrap" align="right">
<font color="blue">286B2P12027600HM</font>
</td>
<td nowrap="nowrap" align="center">02.10.2020</td>
<td nowrap="nowrap" align="right">317.63</td>
<td nowrap="nowrap" align="center">CT</td>
<td nowrap="nowrap" align="left">Получен междубанков превод<br /><br />PO F 303<br />.</td>
<td align="center">
<table>
<tr>
<td align="right" nowrap="nowrap">test iban</td>
</tr>
<tr>
<td align="right" nowrap="nowrap">test</td>
</tr>
</table>
Considerations
.getElement() is not a function. I consider this the root cause of your problems since you won't be able to call an nonexistent method.
Here is the XmlService's Document Class documentation please refer to it when adjusting the example in this answer to your solution.
Code snippet
This is how to get the first row's cells text: (table's headers)
var html = 'the html code you posted in the question';
var parsed = XmlService.parse(html);
Logger.log(
parsed.getRootElement()
.getChildren('tr')[0] // Gets the first row (headers)
.getChildren('td')
.map(cell => cell.getText())
); //[Process date, Reference, Value date, Amount, Type, Description, details BISERA]
Reference
XmlService Document
XmlService
I have created a web application which fetch data from RTC tool.
So In that web application I have to calculate difference at runtime(for third column) basis on two column values.
First column value will be taken from RTC tool programmatically and for second column user will enter value in text boxes and for third column it will calculate difference automatically.
Let me know if we can calculate difference for this third column automatically and how?
Thanks
Hard to tell without rendered HTML. If your columns are in a table, you can do
const makeNum = str => isNaN(str) || str.trim() === ""? 0:+str;
document.getElementById("table").addEventListener("input", function(e) {
const tgt = e.target;
if (tgt.classList.contains("userInput")) { // <input class="userInput"
const parent = tgt.closest("tr");
const rtc = makeNum(parent.querySelector(".rtc").textContent); // <td class="rtc>value</td>
const val = makeNum(tgt.value);
parent.querySelector(".diff").textContent = rtc - val; // <td class="diff"></td>
}
})
<table>
<thead></thead>
<tbody id="table">
<tr>
<td class="rtc">1000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
<tr>
<td class="rtc">2000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
<tr>
<td class="rtc">3000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
<tr>
<td class="rtc">4000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
<tr>
<td class="rtc">5000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
</table>
If you are using jQuery in you project they you can also try this:
Check the working here
JS:
$('.user-input').on('input', function() {
var static_val = $(this).parent().prev().text();
var user_val = $(this).val();
var diff = parseInt(static_val) - parseInt(user_val);
if(!isNaN(diff)){
$(this).parent().next().text(diff);
}else{
$(this).parent().next().text('');
}
});
I have a HTML table with a checkbox in one of the columns. I want to know how I could get the row data when the user clicks on the checkbox with javascript (without jquery)? Can anyone please help me with this?
Thanks
HTML DOM solves your problem
<script type="text/javascript">
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i=0;
while (i < cols.length) {
alert(cols[i].textContent);
i++;
}
}
</script>
<table>
<tr>
<td><input type="checkbox" onclick="getRow(this)" /></td>
<td>aaa</td>
<td>bbb</td>
</tr>
<tr>
<td><input type="checkbox" onclick="getRow(this)" /></td>
<td>ccc</td>
<td>ddd</td>
</tr>
<tr>
<td><input type="checkbox" onclick="getRow(this)" /></td>
<td>eee</td>
<td>fff</td>
</tr>
</table>
EDIT:
this script will help you more, I think:
function getRow(n) {
var row = n.parentNode.parentNode;
var cols = row.getElementsByTagName("td");
var i = 0;
while (i < cols.length) {
var val = cols[i].childNodes[0].nodeValue;
if (val != null) {
alert(val);
}
i++;
}
}
You could try something like this:
HTML:
<table>
<thead>
<tr><th></th><th>Row Text</th></tr>
</thead>
<tr>
<td><input type="checkbox" /></td>
<td>Test</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>Test 2</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>Test 3</td>
</tr>
</table>
JavaScript:
checkboxes = document.getElementsByTagName("input");
for (var i = 0; i < checkboxes.length; i++) {
var checkbox = checkboxes[i];
checkbox.onclick = function() {
var currentRow = this.parentNode.parentNode;
var secondColumn = currentRow.getElementsByTagName("td")[1];
alert("My text is: " + secondColumn.textContent );
};
}
JSFiddle: http://jsfiddle.net/markwylde/wzPHF/1/
if your select is in td directly, try the following:
sel.onclick = function(){
row = this.parentNode.parentNode
//then what you need
}
Note that first you have to find sel with either document.getElementById() or document.getElementsByTagName()
Also you may need to handle onchange event instead of onclick
This will give you content of row directly(all td in tr)
HTML:
<table>
<tr id="tr1">
<td>
<input type="checkbox" value="chkbox1" id="chk1">
</td>
<td>
Lorem ipsum text
</td>
</tr>
<tr id="tr2">
<td>
<input type="checkbox" value="chkbox2" id="chk2">
</td>
<td>
Lorem ipsum text
</td>
</tr>
</table>
Jquery:
$(document).ready(function(){
$('#chk1, #chk2').on('change',function(){
if($('#chk1').prop('checked') || $('#chk2').prop('checked'))
{
var ids=$(this).parent().parent().html();
alert(ids);
}
else
{
}
});
})
if you're new :
onChange EVENT of the checkbox will call the function that i named "checking"
it will send "This.checked", which means, will send True or false to the function "checking", Then i go in the function get that True or False that was sent, and put it in a variable i called "temp".
HTML: onchange="checking(this.checked)" <--- sending out : "This.checked" ( This = the object that creates the Event onchange, and .checked, is the propriety)
you could even send multiple info like this : onchange="checking(this.checked,this.id)" and so on.
JAVASCRIPT :function checking(temp) <--- this is how it gets the "this.checked" in HTML, and put it in the variable temp.
to receive multiple infos : function checking(temp,temp2) and so on.(name it like you want)
then i run a 'IF' with the variable "temp" and if the value of it = true, then do alert.
Hope it helps, think about it and work it so it fits what you need !
HTML :
<table>
<tr>
<td>
<input type="checkbox" value=""onchange="checking(this.checked)">
</td>
<td>
<label>Something Here</label>
</td>
</tr>
</table>
JAVASCRIPT :
function checking(temp)
{
if(temp == true)
{
alert("Checkbox is checked");
}
else
{
alert("Checkbox is NOT checked");
}
}