簡體   English   中英

如何將 SVG 圖像添加到 pdf

[英]How to add SVG image to pdf

當有人單擊生成 pdf 按鈕時,我想生成 pdf 。 此 pdf 包含突出顯示的一堆正方形的圖像,該圖像與用戶在 web 頁面上的圖像中突出顯示的內容相對應。 每當我嘗試包含 svg 時,我都會得到一個空白 pdf。 有什么幫助嗎?

試用.html:

    <?php
    use Dompdf\Dompdf;

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;
    
    

    if(isset($_POST['send']))
    {

     $output = ' <!DOCTYPE html>
                <svg  width="564" height="409" >
                    <image
                        href="https://media.geeksforgeeks.org/wp-content/uploads/unique-rectangles-formed-using- 
                    n-unit-squares.png"
                        width="564"
                        height="409"
                    />
                    <polygon title="1" points="21,385 24,309 100,309 101,385" />
                    <polygon title="2" points="102,305 23,304 23,228 101,227" />
                    <polygon title="3" points="103,225 26,228 25,149 99,151" />
                    <polygon title="4" points="103,147 102,65 25,70 23,147" />
                </svg>';
      //   $output .= '<img src="data:image/svg+xml;base64,'.base64_encode($svg).'"  width="100" height="100" />';

     require 'C:\Users\Main\vendor/autoload.php';  

     $cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Squares</title></head><body><p>Test Printing...</p></body></html>';
             $dompdf = new Dompdf();
             $dompdf->load_html($output);//body -> html content which needs to be converted as pdf..
             $dompdf->render();
             $dompdf->stream("sample.pdf");

     }?>

腳本.js:

    function handleSVGClick(event) {
    if (event.target.tagName === "polygon") {
      event.target.style.fill = `hsl(${Math.random() * 360}, 90%, 60%)`;
    }
  }

樣式.css:

    polygon {
    stroke-width: 2px;
    stroke: #333;
    fill: transparent;
  }

試試這樣:

試用.html:

    <?php
    require 'C:/Users/Main/vendor/autoload.php';  

    use Dompdf\Dompdf;

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;
    
    

    if(isset($_POST['send']))
    {

     //any embedded images need to be base64 encoded as DataURIs
     $svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
                <svg xmlns="http://www.w3.org/2000/svg" width="564" height="409" >
                    <image
                        href="data:image/jpg;base64,' . base64_encode(file_get_contents('https://media.geeksforgeeks.org/wp-content/uploads/unique-rectangles-formed-using-n-unit-squares.png')) . '"
                        width="564"
                        height="409"
                    />
                    <polygon title="1" points="21,385 24,309 100,309 101,385" />
                    <polygon title="2" points="102,305 23,304 23,228 101,227" />
                    <polygon title="3" points="103,225 26,228 25,149 99,151" />
                    <polygon title="4" points="103,147 102,65 25,70 23,147" />
                </svg>';

     //convert SVG image to an <img /> element with the SVG image as a DataURI
     $output = '<img src="data:image/svg+xml;base64,'.base64_encode($svg).'"  width="564" height="409" />';

     $cart_body='<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Squares</title></head><body><p>Test Printing...</p></body></html>';
     $dompdf = new Dompdf();
     $dompdf->load_html($output); //body -> html content which needs to be converted as pdf..
     $dompdf->render();
     $dompdf->stream("sample.pdf");

     }?>

腳本.js:

  function handleSVGClick(event) {
    if (event.target.tagName === "polygon") {
      event.target.style.fill = `hsl(${Math.random() * 360}, 90%, 60%)`;
    }
  }

樣式.css:

  polygon {
    stroke-width: 2px;
    stroke: #333;
    fill: transparent;
  }

嵌入“原始”SVG (<path...>) 尚未工作,您需要鏈接到外部 SVG 文件,或使用如下 DataURI:

 $html = '<img src="data:image/svg+xml;base64,'. base64_encode($svg). '"...>';

上面的代碼使用$svg作為獨立的 SVG 圖像(而不是用於嵌入 HTML 的 SVG 圖像)。 然后它將其編碼為<img />元素的 DataURI,該元素應呈現為 PDF。 由於 SVG 圖像本身包含嵌入圖像,因此也需要將其編碼為 DataURI

PDF 中的圖像應如下所示:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM