image - 在 Amcharts v4 pdf 导出中包含图像
问题描述
我想在我的 Amcharts pdf 导出中添加徽标和其他图像。版本:Ubuntu 16.04 上的 PHP 7,Amcharts 4。
我关注 了 https://www.amcharts.com/docs/v4/tutorials/generating-multi-content-pdf-export/
我有标题、文本图表和表格。该示例不包括图像,但我在这里查看了 PDFmake: https ://pdfmake.github.io/docs/document-definition-object/images/
在 amCharts 表等中使用以下块:
doc.content.push({
table: {
在 PDFMake 中,语法是:
var docDefinition = {
content: [
{
layout: 'lightHorizontalLines', // optional
table: {
和图像:
var dd = {
content: [
'pdfmake (since it\'s based on pdfkit) supports JPEG and PNG format',
'If no width/height/fit is provided, image original size will be used',
{
image: 'sampleImage.jpg',
},
所以我尝试了:
doc.content.push({
image: 'sampleImage.jpg',
});
并在服务器上放一张名为sampleImage.jpg的图片和php文件在同一个文件夹下
我希望将图像添加到我的 report.pdf 下载中,但它不起作用。任何想法表示赞赏。具体来说,什么是正确的语法,我应该把图像放在哪里?我想避免将图像转换为:data:image/jpeg 格式的麻烦。
解决方案
回答我自己的问题:
所以看起来你必须使用格式“data:image/jpeg;base64”来在你的脚本中包含图像。我用“ https://www.base64-image.de/ ”转换了我的图像。结果是如下代码块:
data:image/jpeg;base64,/9j/4A... lots of data ... QhCABCEIA//2Q==
您将其复制到 js 变量中,如下所示:
smiley = 'data:image/jpeg;base64,/9j/4A... lots of data ... QhCABCEIA//2Q==';
然后输出:
doc.content.push({
image: smiley,
width: 30
});
要将图像放入表格中,我做了:
doc.content.push({
table: {
headerRows: 1,
widths: [ "*", "*", "*", "*","*", "*", "*","*", "*", "*" ],
body: [
[
{image: low, width: 30, colSpan: 3, alignment: 'center'},{ },{ },
{image: ok, width: 30, colSpan: 4, alignment: 'center'},{ },{ },{ },
{image: high, width: 30, colSpan: 3, alignment: 'center'},{ },{ }
],
[
{text: "<?php echo $final_score_show[1]; ?>", alignment: 'center'},
...
]
]
}
});
希望这可以帮助某人。享受 !