jsPDF 问题,对齐和元素以及 .text()
jsPDF problems, aligning and elements and with .text()
我正在使用 jsPDF 将 HTML 的一些代码打印成 pdf。
这是我的功能:
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'A4');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
//source = $('.conteudo_simulacao_final')[0];
pdf.setFontSize(40);
pdf.text(250, 220, 'World test!');
source = '<h1><img src="jsPDF/header.jpg" alt="header" style="width:595.28px; top:0; left: 0;" ></h1>';
source += '<div style="width:400px; >'
source += '<div style="float: left;"><img src="jsPDF/cartao_img.png" style="width:300px;" alt="cartao"></div>'
source += '<div style="float: left;"> TESTSASSAE</div>';
source += '</div>':
source += '<img src="jsPDF/footer.png" alt="footer" style="width:595.28; bot: 0;" ></h1>';
// 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: 0,
bot: 0,
left: 0,
width: 522
}
// 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('Test.pdf');
}, margins);
}
我可以创建一个pdf,我的问题是:
我无法并排打印两个 div。我认为 jsPDF 无法读取样式,有什么办法解决这个问题吗?
我的另一个问题是,当我这样做时:'pdf.text(250, 220, 'World test!');'文字在我的图片后面,有什么办法可以把它放在前面吗?
谢谢你们抽出时间。
对于您的 CSS 未呈现的问题,我认为这应该有所帮助:Exporting PDF with jspdf not rendering CSS。
回答者推荐使用Html2Canvas。然后你可以添加 HTML2Canvas JS 然后使用 pdf.addHTML() 而不是 pdf.fromHTML().
图像显示在文本上方的原因是您没有告诉 jsPDF 在页面下方打印。
尝试执行以下操作。最初设置边距,在指定的边距处书写文本,然后增加边距,以便在打印 html 时它会位于 'World Test!' 文本下方。
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'A4');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
//source = $('.conteudo_simulacao_final')[0];
pdf.setFontSize(40);
var margins = {
top: 75,
bottom: 60,
left: 30,
width: 530
};
pdf.text(margins.left, margins.top, 'World test!');
margins.top = margins.top + pdf.internal.getFontSize();
source = '<h1><img src="jsPDF/header.jpg" alt="header" style="width:595.28px; top:0; left: 0;" ></h1>';
source = '<html><body><h1><img src="jsPDF/header.jpg" alt="header" style="width:595.28px; top:0; left: 0;" ></h1>';
source += '<div style="width:400px; >'
source += '<div style="float: left;"><img src="jsPDF/cartao_img.png" style="width:300px;" alt="cartao"></div>'
source += '<div style="float: left;"> TESTSASSAE</div>';
source += '</div>':
source += '<img src="jsPDF/footer.png" alt="footer" style="width:595.28; bot: 0;" ></h1>';
// 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
}
};
// 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('Test.pdf');
}, margins);
}
我正在使用 jsPDF 将 HTML 的一些代码打印成 pdf。
这是我的功能:
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'A4');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
//source = $('.conteudo_simulacao_final')[0];
pdf.setFontSize(40);
pdf.text(250, 220, 'World test!');
source = '<h1><img src="jsPDF/header.jpg" alt="header" style="width:595.28px; top:0; left: 0;" ></h1>';
source += '<div style="width:400px; >'
source += '<div style="float: left;"><img src="jsPDF/cartao_img.png" style="width:300px;" alt="cartao"></div>'
source += '<div style="float: left;"> TESTSASSAE</div>';
source += '</div>':
source += '<img src="jsPDF/footer.png" alt="footer" style="width:595.28; bot: 0;" ></h1>';
// 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: 0,
bot: 0,
left: 0,
width: 522
}
// 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('Test.pdf');
}, margins);
}
我可以创建一个pdf,我的问题是: 我无法并排打印两个 div。我认为 jsPDF 无法读取样式,有什么办法解决这个问题吗?
我的另一个问题是,当我这样做时:'pdf.text(250, 220, 'World test!');'文字在我的图片后面,有什么办法可以把它放在前面吗?
谢谢你们抽出时间。
对于您的 CSS 未呈现的问题,我认为这应该有所帮助:Exporting PDF with jspdf not rendering CSS。
回答者推荐使用Html2Canvas。然后你可以添加 HTML2Canvas JS 然后使用 pdf.addHTML() 而不是 pdf.fromHTML().
图像显示在文本上方的原因是您没有告诉 jsPDF 在页面下方打印。
尝试执行以下操作。最初设置边距,在指定的边距处书写文本,然后增加边距,以便在打印 html 时它会位于 'World Test!' 文本下方。
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'A4');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
//source = $('.conteudo_simulacao_final')[0];
pdf.setFontSize(40);
var margins = {
top: 75,
bottom: 60,
left: 30,
width: 530
};
pdf.text(margins.left, margins.top, 'World test!');
margins.top = margins.top + pdf.internal.getFontSize();
source = '<h1><img src="jsPDF/header.jpg" alt="header" style="width:595.28px; top:0; left: 0;" ></h1>';
source = '<html><body><h1><img src="jsPDF/header.jpg" alt="header" style="width:595.28px; top:0; left: 0;" ></h1>';
source += '<div style="width:400px; >'
source += '<div style="float: left;"><img src="jsPDF/cartao_img.png" style="width:300px;" alt="cartao"></div>'
source += '<div style="float: left;"> TESTSASSAE</div>';
source += '</div>':
source += '<img src="jsPDF/footer.png" alt="footer" style="width:595.28; bot: 0;" ></h1>';
// 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
}
};
// 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('Test.pdf');
}, margins);
}