Im trying to get my jspdf to not save a blank page PDF. I been trying many examples, but nothing works :(. My table content is saved correct, with a image on the second page of the PDF, but my first page is blank.
var pdf = new jsPDF('o', 'pt', 'a6');
//pdf.autoTable(this.columns, this.data);
//var width = pdf.internal.pageSize.width;
//var height = pdf.internal.pageSize.height;
pdf.addPage('1800','900');
pdf.addImage(imgData, 'PNG', 120, 40, 120, 100);
pdf.setTextColor(0,0,0);
pdf.text(120, 20, 'BOOKINGS');
pdf.setFontSize(22);
// 'o', 'pt', 'a4'
// 'p', 'pt', 'letter'
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
source = jQuery('.dataTables_wrapper')[0];
// we support special element handlers. Register them with jQuery-style
// ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
// There is no support for any other type of selectors
// (class, of compound) at this time.
specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true
}
};
margins = {
top: 120,
bottom: 0,
left: 0,
width: 2000
};
// all coords and widths are in jsPDF instance's declared units
// 'inches' in this case
pdf.fromHTML(
source, // HTML string or DOM elem ref.
margins.left, // x coord
margins.top, { // y coord
'width': margins.width, // max width of content on PDF
'elementHandlers': specialElementHandlers
},
function (dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
pdf.save('bookings.pdf');
}, margins);
}
The first page is blank because adding a page is the first call you make after calling the jsPDF constructor. The constructor does already create a (first) blank page. To get rid of the additional empty page at the start of the document, either delete the first page calling doc.deletePage(1), or do not add a page after the constructor is being called.
Related
hello i am trying to generate multiple page dynamic pdf using html in my react js app. i got all the things right in pdf but the last line text of pages cuts if we set the dynamic pixels for html element it also cut table inside the html sometimes. here is my code
var pdf = new jsPDF({
orientation: "portrait", // landscape or portrait
unit: "mm",
format: "a4"//[250,250],
}
)
let pageHeight=pdf.internal.pageSize.height
pdf.html(html_element, {
callback: function (doc) {
doc.save();
},
x: 15,
autoPaging: "slice",
y: 15,
width: 170, //target width in the PDF document
windowWidth: 650 ,
margin:[10,7,10,7],
});
please help me with this
i have also tried jsdpf html2 convas examples but it did not give bottom margin in pages
I'm trying to implement jsPDF pdf saving in a Vue app and I'm finding it overwhelming and nearly impossible to affect any visual change in the output.
The current results are: gigantic text and huge images.
Is there a way to get it to respect any of my CSS, #mediap print or otherwise, particularly for div borders?
The docs are very deep so I'd love an example of how to take a div-based table-like layout—e.g. a shopping cart—and fit it to width. I'd also love tips on avoid content bleeding across page breaks.
I've tried doc.setFont, doc.setFontSize and other methods and nothing changes the output.
this.doc.html(document.getElementById("pdfList"), {
callback: function (doc) {
doc.save("Cycad Inventory");
},
x: 40,
y: 40,
});
exportPDF = () => {
const unit = "pt";
const size = "A4"; // Use A1, A2, A3 or A4
const orientation = "portrait"; // portrait or landscape
const marginLeft = 30;
const doc = new jsPDF(orientation, unit, size);
doc.setFontSize(14);
const title = "Report";
const headers = [["Time", "Source", "Destination", "Protocol", "Length"]];
const data = this.state.ipData.map(elt => [elt.time, elt.source, elt.destination, elt.protocol, elt.length]);
let content = {
startY: 50,
head: headers,
body: data,
}
doc.text(title, marginLeft, 40);
doc.autoTable(content);
doc.save(this.state.ipData.fileName);
}
This is a code I used in reactJS. I hope this will help you to do changes to your code. Here I have declared the height, width, font sizes margins.
I am using jsPDF to create a PDF download.
Below is my code, which works perfectly to display all the bits of info I need so far.
I want everything to display on the PDF in the order I have the code:
Banner
Logo
Job Title
Main Content
Everything sits exactly where it should except for the Banner.
It keeps displaying below all of the content.
In the output PDF I can see the space at the top where it should sit, but it always goes to the last page of the document, after all other content. On the page it displays, it sits in the top left corner and is the correct width and height, so it is doing that correctly, but is just on the wrong page.
What have I stuffed up here? Or how can i force it to sit on page 1?
Thanks in advance for any assistance!
function demoFromHTML() {
var doc = new jsPDF('p', 'mm', 'a4');
var lMargin=20; //left margin in mm
var rMargin=20; //right margin in mm
var pdfInMM=210; // width of A4 in mm
var source = document.getElementById('text-7');
// Banner
html2canvas(jQuery("#top-banner"), {
useCORS : true,
onrendered: function(canvas) {
var banner = canvas.toDataURL('image/png');
doc.addImage(banner, 'PNG', 0, 0, 210, 100);
console.log(banner);
setTimeout(function () {
doc.save('"<?php echo ( get_the_title() );?>"');
}, 0);
}
})
// Logo
var logo = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAC+CAYAAADDYv00AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAGhlJREFUeNrsnc2V28aygMu+3psZGI5AWL6dcCPQePd2giMQHYGgCChFQCkCjiIgJwJKEZDevR3HEfgNjsBjakzOoLur+gf8vnNwfDWXJIDq6vrrPxEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACA3PkPIgAAAENmD9f/PFz1w/W/J3/7P0Sjyw+IAAAi0zxc1XA95stw7RFT0fTO+/XDdXOhnWVo408P1/uH6x6RAQCUkaHNH67Vw/X3yGs7fGeG+Ipz5GuHdj62dY3orjdDrwYF6K9fTiLAZuT398PVR4VfT/69KVQG90NWs0Glr4r24Xo16P3sUZb7+eH6mDjT7XX07fCcvvS6/W7I4iBvuqG9fdv5VzL164n65kPkd3CM/lyvPlpcyLdSUY7ZQfNEBLwb5ATT7w/bEbp8CHSmocZds1+uydazrsCsldoYJmy0FoOT+jvhtZY8Sn/9/ceWLJeoz6T7hWtQ20TW063YBds49fycuWZ7t4h0WrRK0Z7FtUxkUHw6zQJVmiQ+xnOXcbCBU8eZk6VPkJsMsvEx1yFyxiMBnaZCrSYX7PrqrfWkoyqCMydYzYulUftitwqmyjgjT2kgjywCnpHx9GkREvC2hWVqOQ0jwL+ZG7YtdguliH7FKGNWZDIwUAfqQpdp0Elp9vp08blrhYjLwmWCV86XdZYQWtLCoU+HLlOHXsv0q2QQtxpzQMx+/JhIIfro+mYC8quM5dQG/sZfqPhk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Pol7Jq11PWyM4BAIcOoIxLds2EOAAAgExpJPyoU9dT1irEDgBk6AB54eqc2SEOAHDoAEYZekyHvkHkAIBDB0jHXikguEOUAIBDB0jHpUzc9VAWMnQAwKEDJKZ2cPSXsnyWrAEADh0gMc1IJ3+JW0QIAABgQyvjl5vtzjh4l+VqNeIGAACwy7pdnHI3fK/fTGbr8L01ogYAALCjFveNYXyuFlEDAADYYu3Md4gYAADAHpfSuc91g4gBAADsWRo6c8bOAQAAItEaOnRmtgMAAERiZuTMF4gWAAAgLtpl9524n5MOAAAAgVTKDr1BpAAAAGnohFI7AADAJAgtvS8RIQAAQNlOncwcAAAgMxr5toZ87FrzBpEBAFzmB0QAiakGZ93/9+XJ3/cP11f5diTqHjEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACUzv8LMAC5publ4dAlHAAAAABJRU5ErkJggg==';
doc.addImage(logo, 'PNG', 10, 100, 50, 18);
// Job title
doc.setTextColor(0,0,0);
doc.fromHTML(
jQuery('.job_listing-title').get(0), // [Refer Exact code tutorial][2]HTML string or DOM elem ref.
10, // x coord
125, // y coord
{
'width': 190,
});
// Main content
doc.fromHTML(
jQuery('#main').get(0), // [Refer Exact code tutorial][2]HTML string or DOM elem ref.
10, // x coord
135, // y coord
{
'width': 190,
});
}
After posting, I realised how simple the answer was.
The issue was occurring because I was firing the doc.save inside the html2canvas, prior to the logo, job title or main content. Instead I needed to move all of the other content under the banner, but before the doc.save.
The below now functions correctly and outputs all items in the correct order and positions.
function demoFromHTML() {
var doc = new jsPDF('p', 'mm', 'a4');
var lMargin=20; //left margin in mm
var rMargin=20; //right margin in mm
var pdfInMM=210; // width of A4 in mm
var source = document.getElementById('text-7');
html2canvas(jQuery("#top-banner"), {
useCORS : true,
onrendered: function(canvas) {
var banner = canvas.toDataURL('image/png');
// Banner
doc.addImage(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KEDJKZ2cPSXsnyWrAEADh0gMc1IJ3+JW0QIAABgQyvjl5vtzjh4l+VqNeIGAACwy7pdnHI3fK/fTGbr8L01ogYAALCjFveNYXyuFlEDAADYYu3Md4gYAADAHpfSuc91g4gBAADsWRo6c8bOAQAAItEaOnRmtgMAAERiZuTMF4gWAAAgLtpl9524n5MOAAAAgVTKDr1BpAAAAGnohFI7AADAJAgtvS8RIQAAQNlOncwcAAAgMxr5toZ87FrzBpEBAFzmB0QAiakGZ93/9+XJ3/cP11f5diTqHjEBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACUzv8LMAC5publ4dAlHAAAAABJRU5ErkJggg==';
doc.addImage(logo, 'PNG', 10, 100, 50, 18);
// Job title
doc.setTextColor(0,0,0);
doc.fromHTML(
jQuery('.job_listing-title').get(0), // [Refer Exact code tutorial][2]HTML string or DOM elem ref.
10, // x coord
125, // y coord
{
'width': 190,
});
// Main content
doc.fromHTML(
jQuery('#main').get(0), // [Refer Exact code tutorial][2]HTML string or DOM elem ref.
10, // x coord
135, // y coord
{
'width': 190,
});
console.log(banner);
setTimeout(function () {
doc.save('"<?php echo ( get_the_title() );?>"');
}, 0);
}
})
}
I just implemented jspdf in a page, where users work with an editor and then want to be able to print the result. When I tested some example content, I run in to that problem the images are not displayed in there orignial size. They are scaled/displayed so large, that they are lager then the width of the page.
Also I had a problem with the page height/margin-bottom. If the content is to large for a page, then there will be no margin at the bottom of the page. So if for what ever reason an elemtent seems to be too large for a page, this element goes until the end of the page.
I googled a lod, but I couldn't find a solution to avoid that too large elements go until the border of the page.
And I also could not find something, for how to set an image to it's original or max-size when I don't know the content of the page.
Here is js code and an example page: jsfiddle-example
$(".printPdf").click(function () {
var pdfdoc = new jsPDF("portrait");
var getPageTitle = $("#page-title");
var pageTitle = (getPageTitle != "")? getPageTitle : "Document";
var contentClass = ".page-content";
var $pageContent = $(contentClass);
var margins = {
top: 10,
bottom: 20,
left: 15,
width: 180
};
pdfdoc.setDisplayMode('original');
pdfdoc.fromHTML($pageContent.get(0), margins.left, margins.top, {
"width": margins.width,
"text-align": "unset"
}, function () { pdfdoc.save(pdfdoc.pageTitle + ".pdf") }, margins);
});
i have a question to jsPDF autotable.
My Code:
$('#printBtn').on('click', function() {
var pdf = new jsPDF('p', 'pt', 'a4');
var res = pdf.autoTableHtmlToJson(document.getElementById("tablePrint"));
pdf.autoTable(res.columns, res.data, {
theme : 'plain',
styles: {
fontSize: 12
},
showHeader: 'never',
createdCell: function(cell, data) {
var tdElement = cell.raw;
if (tdElement.classList.contains('hrow')) {
cell.styles.fontStyle = 'bold';
}
}
});
pdf.save("test.pdf");
});
I want add Text before and after the Table from a div.
I have found this Code Snippet in jsPDF autotable examples:
var before = "text before";
pdf.text(before, 14, 30);
This code works good. I have insert that before pdf.autoTable(...});.
But i dont know what the number 14 and 30 for?
Then i have the code insert after the pdf.autoTable function call and the Text printed on the last page of pdf but on the top of the page, not on the end, why?
Sorry for my bad englisch.
Thanks for help.
If what you want is to add something before you must first move the table that you are adding with autotable, you achieve this by adding a startY: 150 attribute within doc.autotable:
pdf.autoTable(res.columns, res.data, {
theme : 'plain',
styles: {
fontSize: 12
},
startY: 150,
showHeader: 'never',
createdCell: function(cell, data) {
var tdElement = cell.raw;
if (tdElement.classList.contains('hrow')) {
cell.styles.fontStyle = 'bold';
}
}
});
150 is the value in pixels you want to move. Above this you can place the text you want with the code you placed.
var before = "text before";
pdf.text(before, 14, 30);
Now the values of 14 (Value in Y) and 30 (Value in Y) are the values that you want the text to move in pixels.
In order for you to add text after the table you must first obtain in which number of pixels your table ended and from there enter the text you want.
let finalY = pdf.previousAutoTable.finalY; //this gives you the value of the end-y-axis-position of the previous autotable.
pdf.text("Text to be shown relative to the table", 12, finalY + 10); //you use the variable and add the number of pixels you want it to move.
Here's my stab at an answer:
So this is your Autotable's function call:
var pdf = new jsPDF('p', 'pt', 'a4');
Since jsPDF Autotables is based on jsPDF, you'll have to go here:
pt is a unit of measurement called points, so 14 and 30 are points. At the first position, 14, moves elements left and right. The second position, 30, moves elements down and up. I'm guessing they're like pixels(px). Seems like you have to move your text to your desired locations by using points.
An idea that helped me is to placing a Table with no content on the position where you would like to add the new table.
autoTable(doc, {
body: [],
startY: finalY + bias
});