首页 > 解决方案 > 如何将特殊字符导出为 PDF

问题描述

我在使用 jsPDF 将一些特殊字符(塞尔维亚拉丁字母)导出到 PDF 时遇到问题。

我已经应用了转换后的字体并设法使它适用于简单的文本,但是在导出 id="exportContent" 的 div 元素的内容时我无法使它工作。

这是工作并导出带有特殊字符的简单文本的代码:

function demoFontTypes() {
    var doc = new jsPDF();
    doc.setFont('ARIALUNI'); // set font
    doc.setFontSize(10);
    doc.text('žsćdšclsdpžcslcpsžclpsdpžclsdžpclsdžclpsć', 10, 10);
    doc.save('test.pdf');
}

我正在尝试使用此代码导出 div 元素的内容,但无法导出这些字符(žćđšč)

$(document).ready(function() {
    $(".btnPDF").click(function() {
    var doc = new jsPDF("p", "pt", "A4");
    var source = $("#exportContent")[0];
    var margins = {
        top: 30,
        bottom: 60,
        left: 50,
        width: 522
    };
    doc.setFont('ARIALUNI'); // set font
    doc.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
        },
        function(dispose) {
            // dispose: object with X, Y of the last line add to the PDF
            //          this allow the insertion of new lines after html
            doc.save("Test.pdf");
        },
        margins
    );
    });
});

我需要能够使用特殊字符(塞尔维亚拉丁语)导出 div 元素的内容

标签: javascriptjspdf

解决方案


推荐阅读