将base64字符串嵌入到pdf PHP中

时间:2014-04-24 05:53:01

标签: php jquery post base64

我试图将图表发送到服务器以从中创建pdf

       <form id="pdfform"> <input type="hidden" id="imgbase64" name="imgbase64" > </form> 


var dataURL = canvas.toDataURL('image/png');    
canvas.parentNode.removeChild(canvas);


       $('#imgbase64').val(dataURL);   
       $('#pdfform').get(0).setAttribute('action', 'controller/sendto_pdf.php');
       $('#pdfform').get(0).setAttribute('type', 'post');
       $('#pdfform').submit();  

即使它发布了,我也会收到此错误?

Request-URI太大

请求的网址长度超出了此服务器的容量限制。

&#34; msound&#39;评论&#34;帮我找到字符串发送后,我有我的字符串如何将其嵌入到PDF中我尝试了一些方法,但我无法在PDF中获取图像

// Include the main file containing the mpdf class:
include('../classes/MPDF56/mpdf.php');



$base64_string = $_POST['imgbase64'];
$data_img = explode(',', $base64_string);
$data_img_dec = base64_decode($data_img[1]);

function base64_to_jpeg($base64_string, $output_file) {
    $ifp = fopen($output_file, "wb"); 

    $data = explode(',', $base64_string);

    fwrite($ifp, base64_decode($data_img[1])); 
    fclose($ifp); 

    return $output_file; 
}

$image = base64_to_jpeg( $base64_string, 'tmp.png' ); 

//if($_POST) print_r($_POST);

//
//
// Create an instance of the class:
//$logo="../images/logo_Inet_Sm.png";
$mpdf=new mPDF();
$stylesheet = file_get_contents('../css/pdf.css');
$mpdf->WriteHTML($stylesheet,1);
// Write some HTML code:
$mpdf->WriteHTML('




<div id="container">
   <h4 id ="page_title"><img src="../images/logo_mercury_smaler.jpg"  />  </h4>  
<br />
<br />
<img src="'.$data_img_dec.'" />
 <img src="'.$image.'" />
     <img src="data:image/x-icon;base64,'.$base64_string.'" />
</div>


        ',2); //end pdf html

我已经尝试将图像保存到磁盘然后将其添加到pdf但是当我这样做时该文件是0kb

   $base64_string = $_POST['imgbase64'];


function base64_to_png($base64_string, $output_file) {
    $ifp = fopen($output_file, "wb"); 

    $data = explode(',', $base64_string);

    fwrite($ifp, base64_decode($data_img[1])); 
    fclose($ifp); 

    return $output_file; 
}

$image = base64_to_png( $base64_string, 'tmp.png' ); 
file_put_contents('/var/www/html/inet/tools/reports/controller/tmpimg/tmp.png',     file_get_contents($image));

我找到了将图像保存到磁盘所需的正确代码,并且可以在浏览器中查看,但是当我尝试将其添加到pdf时图像没有显示,我确实得到了另一个图像标志

<?php

// Include the main file containing the mpdf class:
include('../classes/MPDF56/mpdf.php');



$base64_string = $_POST['imgbase64'];

$data = $base64_string;

list($type, $data) = explode(';', $data);
list(, $data)      = explode(',', $data);
$data = base64_decode($data);

file_put_contents('tmpimg/chart_image.png', $data);


sleep(2);


$mpdf=new mPDF();
$stylesheet = file_get_contents('../css/pdf.css');
$mpdf->WriteHTML($stylesheet,1);
// Write some HTML code:
$mpdf->WriteHTML('

<div id="container">
<h4 id ="page_title">
<img src="../images/logo_mercury_smaler.jpg"  /> Custom reports and charts 
</h4>  
<br />
<br />

<img src="tmpimg/chart_image.png" />

</div>

',2); //end pdf html


// Output a PDF file:
$mpdf->Output();
// exec('rm -f tmpimg/chart_image.png');
sleep(2);
exit;

我尝试添加一些睡眠,认为在我使用它之前需要几秒钟来保存图像

0 个答案:

没有答案