我正在使用d3.js创建图表作为SVG.这些图表是根据经过身份验证的用户的选择动态生成的.生成这些图表后,用户可以选择将生成的SVG下载为PNG或PDF.
目前的工作流程如下:
// JAVASC
// get the element containing generated SVG
var svg = document.getElementById("chart-container");
// Extract the data as SVG text string
var svg_xml = (new XMLSerializer).serializeToString(svg);
// Submit the
to the server.var form = document.getElementById("svgform");
form['output_format'].value = output_format; // can be either "pdf" or "png"
form['data'].value = svg_xml ;
form.submit();
FORM元素是一个隐藏的表单,用于POST数据:
PHP文件将提供的SVG数据保存为临时文件:
// check for valid session, etc - omitted for brevity
$xmldat = $_POST['data']; // serialized XML representing the SVG element
if(simplexml_load_string($xmldat)===FALSE) { die; } // reject invalid XML
$fileformat = $_POST['output_format']; // chosen format for output; PNG or PDF
if ($fileformat != "pdf" && $fileformat != "png" ){ die; } // limited options for format
$fileformat = escapeshellarg($fileformat); // escape shell arguments that might have snuck in
// generate temporary file names with tempnam() - omitted for brevity
$handle = fopen($infile, "w");
fwrite($handle, $xmldat);
fclose($handle);
运行转换实用程序,该实用程序读取临时文件($infile)并在指定的$fileformat(PDF或PNG)中创建新文件($outfile).然后,生成的新文件将返回到浏览器,并删除临时文件:
// headers etc generated - omitted for brevity
readfile($outfile);
unlink($infile); // delete temporary infile
unlink($outfile); // delete temporary outfile
所以:
在将convert.php写入文件之前,如何最好地清理或过滤提供给convert.php的SVG数据? SVG清理的当前状态是什么? PHP中有什么可用的?我应该使用whitelist-based approach来清理提供给conversion.php的SVG数据,还是有更好的方法?
(我不知道XSLT,虽然我可以尝试学习它;我希望尽可能地在PHP中保持清理.使用Windows Server 2008,因此任何使用外部工具的解决方案都需要在该生态系统中可用.)