将图片放入pdf TCPDF

时间:2019-05-08 04:51:57

标签: php tcpdf

我正在尝试生成同时显示图像的pdf。因此,我的图像存储在数据库中,并且有一个工作代码可以将数据库中的数据生成为PDF文件。

这是将我的图像从数据库查看到数据表的代码

<td ><img  onclick="onClick(this)" class="w3-hover-opacity" src="<?php echo base_url('uploads/'.$report["attachment"].'.png'); ?>"  style="width:100%;max-width:200px;border-radius: 5px" ></td> 

这是我所做的,但只打印img文件名

if(isset($_POST["generate_pdf"]))  
 {  


      require_once('tcpdf/tcpdf.php');  
      $obj_pdf = new TCPDF('L', 'mm', 'LETTER', true, 'UTF-8', false);
      $obj_pdf->SetCreator(PDF_CREATOR);  
      $obj_pdf->SetAuthor('Nicola Asuni');
      $obj_pdf->SetSubject('TCPDF Tutorial');
      $obj_pdf->SetKeywords('TCPDF, PDF, example, test, guide');
      $obj_pdf->SetTitle("Incident Report");  
      $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
      $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
      $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
      $obj_pdf->SetDefaultMonospacedFont('helvetica');  
      $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
      $obj_pdf->SetMargins(2, '7', PDF_MARGIN_RIGHT, '10');  
      $obj_pdf->setPrintHeader(false);  
      $obj_pdf->setPrintFooter(false);  
       $obj_pdf->setPrintFooter(false);
      $obj_pdf->SetFont('helvetica', '', 5);  
      $obj_pdf->AddPage();  

      date_default_timezone_set("Asia/Manila");
      $content = '';  
        $content2 = date("F d, Y h:i:s A",mktime());
      $content .= '
      <h4 align="center">Incident Report</h4> <br /> 

      <table align="center" border="1" cellspacing="0" cellpadding="3">  
           <tr>  

                <th width="8%">School</th>  
                <th width="5%">Grade/Level</th> 
                 <th width="6%">Section</th> 
                <th width="7%">Date and Time</th>  
                <th width="6%">Incident Type</th>  
                <th width="7%">Incident Description</th>
                   <th width="6%">Sender</th>  
                <th width="10%">Person Involved</th>   
                   <th width="14%">Students that tagged safe</th> 
                   <th width="14%">Students that tagged unsafe</th> 
                      <th width="4%">Male Count</th> 
                      <th width="5%">Female Count</th> 
                   <th width="5%">Number of involved people</th>
                  if(isset($_POST["generate_pdf"]))  
 {  


      require_once('tcpdf/tcpdf.php');  
      $obj_pdf = new TCPDF('L', 'mm', 'LETTER', true, 'UTF-8', false);
      $obj_pdf->SetCreator(PDF_CREATOR);  
      $obj_pdf->SetAuthor('Nicola Asuni');
      $obj_pdf->SetSubject('TCPDF Tutorial');
      $obj_pdf->SetKeywords('TCPDF, PDF, example, test, guide');
      $obj_pdf->SetTitle("Incident Report");  
      $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
      $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
      $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
      $obj_pdf->SetDefaultMonospacedFont('helvetica');  
      $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
      $obj_pdf->SetMargins(2, '7', PDF_MARGIN_RIGHT, '10');  
      $obj_pdf->setPrintHeader(false);  
      $obj_pdf->setPrintFooter(false);  
       $obj_pdf->setPrintFooter(false);
      $obj_pdf->SetFont('helvetica', '', 5);  
      $obj_pdf->AddPage();  

      date_default_timezone_set("Asia/Manila");
      $content = '';  
        $content2 = date("F d, Y h:i:s A",mktime());
      $content .= '
      <h4 align="center">Incident Report</h4> <br /> 

      <table align="center" border="1" cellspacing="0" cellpadding="3">  
           <tr>  

                <th width="8%">School</th>  
                <th width="5%">Grade/Level</th> 
                 <th width="6%">Section</th> 
                <th width="7%">Date and Time</th>  
                <th width="6%">Incident Type</th>  
                <th width="7%">Incident Description</th>
                   <th width="6%">Sender</th>  
                <th width="10%">Person Involved</th>   
                   <th width="14%">Students that tagged safe</th> 
                   <th width="14%">Students that tagged unsafe</th> 
                      <th width="4%">Male Count</th> 
                      <th width="5%">Female Count</th> 
                   <th width="5%">Number of involved people</th>
                  <th width="5%">Attachment</th>
           </tr>';  
      $content .= fetch_data();  
      $content .= '</table>';  


      $obj_pdf->writeHTML($content);  
      $obj_pdf->writeHTML($content2);
      $obj_pdf->Output('report.pdf', 'I');  
 }  
 ?>  
           </tr>';  
      $content .= fetch_data();  
      $content .= '</table>';  


      $obj_pdf->writeHTML($content);  
      $obj_pdf->writeHTML($content2);
      $obj_pdf->Output('report.pdf', 'I');  
 }  
 ?>  

我需要添加另一个$ obj_pdf吗?而且我知道<th width="5%">Attachment</th>行仅显示文件名。我只是在试水

2 个答案:

答案 0 :(得分:0)

您可以通过将图像的base64编码字符串输入标签来设置src标签中的<img>属性来实现此目的。像这样:

只需将此脚本放在您要显示图像的pdf html中即可。

HTML格式:

<img class="w3-hover-opacity" style="width:100%;max-width:200px;border-radius: 5px" src="data:image/png;base64,'<?php echo base64_encode(file_get_contents('uploads/'.$report["attachment"] . '.png')); ?>">

或PHP串联字符串:

echo
'<img class="w3-hover-opacity" style="width:100%;max-width:200px;border-radius: 5px" src="data:image/png;base64,' . base64_encode(file_get_contents('uploads/'.$report["attachment"] . '.png')) . '">;

对于file_get_contents(),请尝试:

file_get_contents(base_url('uploads/'.$report["attachment"] . '.png'))

然后将其包装在base64_encode()中,例如:

base64_encode(file_get_contents(base_url('uploads/'.$report["attachment"] . '.png')))

那应该为您解决问题。

答案 1 :(得分:0)

向您显示代码,我认为这是图像路径问题。 请尝试以下解决方案:

echo '<img class="w3-hover-opacity" style="width:100%;max-width:200px;border-radius: 5px" src="../uploads/'.$report["attachment"].'.png">';

有关其他任何信息,您可以访问此TCPDF Image Example