How to keep some formatting and paragraphs when using jsPDF?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 16:16:43

问题


Okay, so I have a basic table for a schedule I created for a gym. I want to be able to download that table using jsPDF. As of right now, I've been able to get the table downloading fine, however there are a few things I just can't get to work.

First, I cannot, for the life of me, get the table to be full width within the PDF that get's downloaded. I've read dozens of posts about jsPDF and nothing has been able to solve the problem!

Second, the information within each cell is in 3 different p tags and therefore should be on three different lines, but it is jumbling the lines together.

Third, I would like the cells to remain text aligned center.

The jsFiddle below shows as far as I've gotten, but these last three items are really starting to bug me, and I've researched the heck out of it with no luck...

http://jsfiddle.net/xzZ7n/695/

function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter');
source = $('#editor')[0];

specialElementHandlers = {
    // element with id of "bypass" - jQuery style selector
    '#bypassme': function (element, renderer) {
        // true = "handled elsewhere, bypass text extraction"
        return true
    }
};
margins = {
    top: 30,
    bottom: 30,
    left: 40,
    width: '100%'
};

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

    pdf.save('Test.pdf');
}, margins);
}

回答1:


Include html2canvas and then use pdf.addHTML() instead of pdf.fromHTML(). It works great. I used it and it kept all my styles from the page.

Here's my code:

 var pdf = new jsPDF('p', 'pt', 'letter');
 pdf.addHTML($('#NameOfTheElementYouWantToConvertToPdf')[0], function () {
     pdf.save('Test.pdf');
 });


来源:https://stackoverflow.com/questions/25703431/how-to-keep-some-formatting-and-paragraphs-when-using-jspdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!