使用jsPDF将HTML转换为PDF,支持RTL

3

我正在尝试使用angular 5和jsPDF将html转换为pdf。
这是我的代码:

import * as jsPDF from "jspdf";
.
.
.
htmlToPdf(){
var doc=new jsPDF();
var specialElementHandlers = {
  '#content' : function(element,render) {return true;}
};

doc.fromHTML(document.getElementById('content'), 20,20,{
           'width':500,
      'elementHandlers': specialElementHandlers,
});

doc.save('Reports.pdf');
}

我正在尝试将PDF导出为从右到左的方向,但没有成功。
当我尝试使用这个命令时:
doc.viewerPreferences({Direction: 'R2L'});

我遇到了一个错误:'在类型"jsPdf"上不存在该属性'
顺便说一下,在我的html或css中,我尝试使用direction属性,但没有帮助。它仍然是从左到右

1个回答

3
在我的组件中,我使用了这个函数:
htmlToPdf(){
    const elementToPrint = document.getElementById('content');
    const pdf = new jsPDF();
    pdf.addHTML(elementToPrint, () => {
        pdf.save('report.pdf');
    });

在我的HTML中,我使用了一个div标签来包裹所有内容,并将其id设置为“content”,然后使用属性dir="rtl"。
<div id="content" dir="rtl">
.
.
.
</div>

不要忘记添加这个脚本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>

在2021年,语法已经改变。现在使用pdf.html代替pdf.addHtml。函数名称已更改。 - yehonatan yehezkel
1
你说得没错,但我写它已经将近4年了。那些仍在使用Angular 5的人可能还需要使用它。但感谢你注意到了。 - לבני מלכה

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接