【问题标题】:Attach image in a php form and use html2pdf以 php 形式附加图像并使用 html2pdf
【发布时间】:2020-02-03 16:50:59
【问题描述】:

我有一个 php 表单并使用 html2pdf (TCPPDF)。当我使用表单中已经包含的文本和图像时,php 表单有效,但现在我想附加图像并将其包含在 pdf 文件中。

我现在使用的代码得到了文件名。

附上我正在使用的图片
$<span><input type="file" name="foto"></span>
并将其包含在 pdf 文件中,我使用此代码
$foto = $_POST['foto'];
$content .= '<img src='.$foto.'with=200 height=auto>';

对不起,我的英语不好!
问候
丹尼尔

【问题讨论】:

  • 在你的 $_POST 变量上做一个var_dump()...你会看到,没有设置图像。你需要一个 HTML-FORM multipart/form-data 并且在 PHP 中你可以用 $_FILE 阅读它
  • 感谢您的回答。所以我不能只写foto = $_FILES['foto']; 并在 pdf 文件$content .= '<img src='.$foto.'with=200 height=auto>'; 中“打印”它,或者我应该写什么?

标签: php tcpdf html2pdf


【解决方案1】:

像这样上传普通的 HTML 文件:

<form method="post" enctype="multipart/form-data">
  <label>Deine Datei ;) :
    <input name="fileUpload" type="file" accept="image/*"> 
  </label>  
  <label>Your Name:
    <input name="userName" type="text" accept="image/*"> 
  </label>
  <button type="submite" name="submite">Upload</button>
</form>

在 PHP 端,你必须移动你的文件:

if(isset($_POST["submit"])) {
    $userName = $_POST['userName']; // the normale methode
    $check = getimagesize($_FILES["fileUpload"]["tmp_name"]);
    if($check !== false) {
       $temp_path_to_file = $_FILES['fileUpload']['tmp_name'];
       $file_content = file_get_contents($temp_path_to_file); 
       // and now past your image to your PDF.
       // if you want to save the image on the server you must move it with this (its not necessary if you want only to past it in your PDF)
       if(move_uploaded_file($temp_path_to_file, '/my/image/folder/image_'.uniqid() . '.' . pathinfo($filename, PATHINFO_EXTENSION);
    }
}

编辑: 对于您的示例,您必须将 $file_content 转换为 base64 字符串。比你可以在你的图片中粘贴它:

$base64 = 'data:image/' . pathinfo($filename, PATHINFO_EXTENSION) . ';base64,' . base64_encode($file_content);
echo '<img src="'. $base64 .'" style="with: 200; height: auto">';

【讨论】:

  • 感谢您的帮助!当我使用普通的HTML文件上传时,如何在pdf文件中打印? '&lt;img src="'. base64_encode($file_content) .'" style="with: 200; height: auto"&gt;'; 不起作用。
  • “不工作”非常不清楚。什么不起作用?你在$file_content 上做了var_debug 吗?里面有内容吗?请在此处发布您的代码,以便我们更好地帮助您。
  • 我已经粘贴了你为正常的 HTML 文件上传和 php 代码编写的代码,并删除了最后一行 if(move_uploaded_file($temp_path_to_file, '/my/image/folder/image_'.uniqid() . '.' . pathinfo($filename, PATHINFO_EXTENSION); 和当在 $file_content 上做 var_debug 时,我在屏幕:) 但是当我粘贴$content .='&lt;img src="'. base64_encode($file_content) .'" style="with: 200; height: auto"&gt;'; 时,当我按下提交时屏幕变成白色。整个代码无法容纳许多字符。再次感谢您帮助我!
  • 你是最棒的@Richard!当我更新图像部分并按提交时,我想在 pdf 中附加的图像显示在表格顶部,并且在 pdf 中打印data:image/;base64,/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQ.......
猜你喜欢
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 2013-12-21
  • 2016-04-08
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 2014-06-15
相关资源
最近更新 更多