【发布时间】:2018-01-31 20:35:17
【问题描述】:
我正在尝试将SVG 转换为带有pdfmake 的图像。
const svg = document.getElementById('chart-grafico-pizza').childNodes[0];
const img = document.createElement('img');
const canvas = document.getElementById('teste-canvas');
// get svg data
const xml = new XMLSerializer().serializeToString(svg);
// make it base64
const svg64 = btoa(xml);
const b64Start = 'data:image/svg+xml;base64,';
// prepend a "header"
const image64 = b64Start + svg64;
// set it as the source of the img element
img.src = image64;
// draw the image onto the canvas
(canvas as HTMLCanvasElement).getContext('2d').drawImage(img, 0, 0);
图像始终返回为undefined。
【问题讨论】:
-
你有没有在调试器中逐行检查这一行,看看它在什么时候失败了?这样做时你会得到什么输出?
-
@AgiHammerthief 是的,代码正在运行,但返回未定义
-
@AgiHammerthief svg 怎么会这么大,不能转换?
-
我遇到了同样的问题。虽然 pdfmake 文档声明它支持 base64 数据 URI,但它确实在此文档页面的顶部提到仅“支持 JPEG 和 PNG 格式”:pdfmake.github.io/docs/0.1/document-definition-object/images
-
我已经在 pdfmake GitHub repo 上提交了一个问题:github.com/bpampuch/pdfmake/issues/2326
标签: javascript html angular svg pdfmake