首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未将特殊字符导出到PDF

未将特殊字符导出到PDF
EN

Stack Overflow用户
提问于 2017-10-30 08:23:40
回答 2查看 1.3K关注 0票数 0

我使用jspdf将数据导出为PDF。将数据导出到PDF时,特殊字符不会显示在PDF中。

请找到演示插页:https://plnkr.co/edit/BCgDQm3jV9ctwYJMWkjh?p=preview

在上面的演示柱塞中,当用户单击导出按钮时,内容将导出到PDF并下载PDF,但问题是我注意到特殊字符没有导出到PDF。在网页上可以看到的option1,option2提到的项目符号和数字不会导出到PDF。

JavaScript代码:

代码语言:javascript
复制
$scope.export = function() {
    var pdf = new jsPDF('landscape');
    var pdfName = 'test.pdf';

    var options = {};

    var $divs = $('.myDivClass')                //jQuery object of all the myDivClass divs
    var numRecursionsNeeded = $divs.length -1;     //the number of times we need to call addHtml (once per div)
    var currentRecursion=0;

    //Found a trick for using addHtml more than once per pdf. Call addHtml in the callback function of addHtml recursively.
    function recursiveAddHtmlAndSave(currentRecursion, totalRecursions){
        //Once we have done all the divs save the pdf
        if(currentRecursion==totalRecursions){
            pdf.save(pdfName);
        }else{
            currentRecursion++;
            pdf.addPage();
            //$('.myDivClass')[currentRecursion] selects one of the divs out of the jquery collection as a html element
            //addHtml requires an html element. Not a string like fromHtml.
            pdf.addHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
                console.log(currentRecursion);
                recursiveAddHtmlAndSave(currentRecursion, totalRecursions)
            });
        }
    }

    pdf.addHTML($('.myDivClass')[currentRecursion], 15, 20, options, function(){
        recursiveAddHtmlAndSave(currentRecursion, numRecursionsNeeded);
    });
}

任何输入都会很有帮助。

EN

回答 2

Stack Overflow用户

发布于 2017-10-30 13:02:51

根据文档,doc.addHTML正在被弃用,取而代之的是可向量的应用编程接口。这种方法似乎确实缺少一些功能,比如CSS list-style-type的渲染。

我不确定doc.fromHTML是否是这个新的API (目前在文档中还没有),但它似乎能够呈现以下内容:

代码语言:javascript
复制
var doc = new jsPDF();
doc.fromHTML(ul, 15, 15, {
  width: 170
});
doc.addPage();
doc.fromHTML(ol, 15, 15, {
  width: 170
});
var blob = doc.output('blob');
var i = document.createElement('iframe');
i.width = '100%';
i.height = window.innerHeight;
i.src = URL.createObjectURL(blob);
var a = document.createElement('a');
a.download = 'doc.pdf';
a.innerHTML = 'download (for chrome...)';
a.href = i.src;
document.body.appendChild(a);
document.body.appendChild(i);
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.debug.js"></script>

<div id="ul">
  <ul>
    <li>first</li>
    <li>second</li>
  </ul>
</div>
<div id="ol">
  <ol>
    <li>first</li>
    <li>second</li>
  </ol>
</div>

这就是你固定的plnkr

票数 1
EN

Stack Overflow用户

发布于 2017-10-30 13:00:00

当我读到你在github中使用的插件时,你可能需要使用UTF-8字符集来完成它。您可以尝试在head html上添加meta charset UTF-8,这将用于生成PDF

或者如果您使用nodeJS作为后端。你可以试试phantom JS PDF。我以前确实用过它,它甚至支持阿拉伯、中文、俄语、日语等符号语言(有一些配置)。

为提高可读性而进行编辑

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47006636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档