简体   繁体   English

Amcharts中的乱码导出为PDF

[英]Garbled Text in Amcharts Export to PDF

I try to use Javascript to export the amcharts to PDF by following this website's 我尝试通过以下网站的网站使用Javascript将amcharts导出为PDF

[Codepen](https://codepen.io/team/amcharts/pen/35076c4d7b6eef7764dacc61f72adadc)

steps, but the PDF has garbled text(Chinese words), I want to use UTF-8, how can I fix it? 步骤,但是PDF的文本(中文单词)乱码,我想使用UTF-8,如何解决? And why the chart's degree of resolution is so low? 为什么图表的分辨率如此之低? Thanks. 谢谢。 enter image description here 在此处输入图片说明

html html

<h2 id="OPASS" class="display-5 col-md-offset-4 col-md-12">測試圖表</h2>

javascript javascript

    var downloadPDF = function() {
            console.log("Starting export...");
            var ids = ["chartdiv", "Passion_chart", "Anchor_chart", "Switch_chart", "Synergy_chart"];
            var charts = {}, charts_remaining = ids.length;
            for (var i = 0; i < ids.length; i++) {
                for (var x = 0; x < AmCharts.charts.length; x++) {
                    if (AmCharts.charts[x].div.id == ids[i])
                        charts[ids[i]] = AmCharts.charts[x];
                }
            }
            for (var x in charts) {
                if (charts.hasOwnProperty(x)) {
                    var chart = charts[x];
                    chart["export"].capture({}, function() {
                        this.toJPG({}, function(data) {
                            // Save chart data into chart object itself
                            this.setup.chart.exportedImage = data;
                            // Reduce the remaining counter
                            charts_remaining--;
                            // Check if we got all of the charts
                            if (charts_remaining == 0) {
                                // Yup, we got all of them
                                // Let's proceed to putting PDF together
                                generatePDF();
                            }
                        });
                    });
                }
            }
            function generatePDF() {
                // Log
                console.log("Generating PDF...");
                // Initiliaze a PDF layout
                var layout = {
                    "content": []
                };
                // Let's add a custom title
                layout.content.push({
                    "text": document.getElementById("OPASS").innerHTML,
                    "fontSize": 24
                });
                // Now let's grab actual content from our <p> intro tag
                layout.content.push({
                    "text": document.getElementById("intro").innerHTML
                });
                // Add bigger chart
                layout.content.push({
                    "image": charts["chartdiv"].exportedImage,
                    "fit": [523, 300]
                });
                chart["export"].toPDF(layout, function(data) {
                this.download(data, "application/pdf","amCharts.pdf");
            });
        }
    }

PDFMake, the library AmCharts leverages for PDF export, does not support Chinese out of the box with the default Roboto font it's using (there are tons of issues about this in the issue tracker, such as this one ). AmCharts利用PDFMake进行PDF导出,该库不支持使用默认的Roboto字体开箱即用的中文(问题跟踪器中有很多与此相关的问题,例如this )。 You have to create a custom vfs_fonts.js file with a font that has Chinese characters and use that instead of the bundled font file using the step by step tutorial provided by the pdfmake devs. 您必须使用pdfmake开发人员提供的分步教程 ,使用具有中文字符的字体创建自定义vfs_fonts.js文件,并使用该字体而不是捆绑的字体文件。 "Microsoft YaHei" seems to be recommended going by the github issues I looked at. 我看过的github问题似乎建议使用“ Microsoft YaHei”。

Once you've created your custom font file, you need to manually include pdfMake.js into your page, as well as your custom vfs_fonts.js file, then a reference to window.pdfMake.fonts otherwise it knows to use that instead of the default Roboto font. 创建自定义字体文件后,您需要将pdfMake.js以及自定义vfs_fonts.js文件手动添加到页面中,然后引用window.pdfMake.fonts,否则它会知道使用它而不是默认的Roboto字体。

<script src="./amcharts/plugins/export/libs/pdfMake/pdfMake.js" type="text/javascript"></script>
<script src="vfs_fonts.js" type="text/javascript"></script>
<script type="text/javascript">
    pdfMake.fonts = {
        "Microsoft Yahei": {
            "normal": 'name of font.tff', //replace with the name of the ttf font file
            "bold": 'name of bold font.ttf',
            "italics": 'name of italics font.ttf',
            "bolditalics": 'name of bold italics font.ttf'
        }
    }
    // ...

AmCharts.makeChart( {
    "export": {
        "enabled": true,
        "pdfMake": {
            defaultStyle: {
                font: "Microsoft YaHei"
            }
        },
    },
    ...
} );
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM