2017-05-30 24 views
0

我已經在html中使用PHP生成輸出爲svg,並且必須強制下載它作爲svg圖像,當我單擊下載爲svg時。當我點擊下載時如何生成它作爲svg圖像。如何生成svg文件夾並強制下載爲使用php的svg文件?

foreach($items as $item){ 
    // echo $item->top; 
$top  = str_replace('px', '', $item['top']); 
$left  = str_replace('px', '', $item['left']); 
    echo '<div style="background-color:#fff;width:333.333px;height:500px;">'; 
    echo '<div style="position:absolute;top:'.$top.';left:'.$left.'">'; 
    $svg_element = $item['svg']; 
    echo $svg_element; 
    echo '</div>'; 
    echo '</div>'; 
} 

    // output 
    <div style="background-color:#fff;width:333.333px;height:500px;"><div style="position:absolute;top:235;left:64.0667"> 
<svg width="198" height="132" viewBox="0 0 99 52" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="none"> 
<defs><style>@font-face{font-family: 'Twine';src: url("data:application/font-ttf;charset=utf-8;base64,") 
<g id="0.38592716854139286"><text fill="#000000" stroke="none" stroke- width="0" stroke-linecap="round" stroke-linejoin="round" x="32" y="39" text-anchor="middle" font-size="40.30555555555556" font-family="Twine" data-textcurve="0" data-itemzoom="1 1" itemzoom="0.5 0.3939393939393939"> <tspan dy="0" x="50%">Hello</tspan></text></g> 
+0

你有沒有找到任何解決辦法? – Jalpesh

回答

0

對於SVG應該沒有任何div。所以我已刪除的div和寬度和高度,背景顏色創建SVG,並把內容的文件保存爲擴展名爲.svg

$op = '<svg width="'.$template_width.'" height="'.$template_height.'">'; 
foreach($items as $item){ 
    // echo $item->top; 
$top  = str_replace('px', '', $item['top']); 
$left  = str_replace('px', '', $item['left']); 
    $op .= str_replace('<svg ', '<svg y="'.$top.'" x="'.$left.'" ', $item['svg']); 

} 
$op .= '</svg>'; 
$filename = '/front_'.time().'.svg'; 
file_put_contents(realpath(dirname(__FILE__)).$filename, $op);