如何在使用jsPDF时保留一些格式和段落? [英] How to keep some formatting and paragraphs when using jsPDF?

查看:172
本文介绍了如何在使用jsPDF时保留一些格式和段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个为健身房创建的时间表的基本表格。我希望能够使用jsPDF下载该表。截至目前,我已经能够下载表格,但是有一些事情我不能上班。



首先,我在我的生活中,不能在下载的PDF中获得全宽的表格。我已经阅读了几十篇关于jsPDF的文章,没有什么能够解决这个问题的!

第二,每个单元格内的信息有3个不同的 p 标签,因此应该位于三条不同的线上,但它们混在一起。

第三,我希望单元格保持文本对齐中心。



下面的jsFiddle显示了我所得到的结果,但最后三个项目真的开始让我烦恼,而且我已经研究了这个问题,没有运气......



http:// jsfiddle。 net / xzZ7n / 695 /

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

specialElementHandlers = {
// id为bypass的元素 - jQuery样式选择器
'#bypassme':function(element,renderer){
// true =在其他地方处理,绕过文本提取
return true
}
};
margin = {
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,// PDF上内容的最大宽度$ b $'elementHandlers':specialElementHandlers
},

function(dispose){

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


解决方案

Include html2canvas ,然后使用 pdf.addHTML()而不是 pdf.fromHTML()。它效果很好。我使用它,它保留了页面中的所有样式。



这里是我的代码:

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


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);
}

解决方案

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');
 });

这篇关于如何在使用jsPDF时保留一些格式和段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆