doulingzou1712 2013-10-04 14:35 采纳率: 0%
浏览 72
已采纳

我可以使用dompdf将发布的图像文件注入PDF吗?

I have a form where users can fill input boxes. In some cases, they're allowed to select images too.

The problem is, I don't want to save the image file somewhere on our server and give PDF the URL like this:

//Put uploaded file on public/uploads/ folder
<img src="{{ $imagePath }}" style="height: 50px; width: 50px;">

Instead, I'd like to inject uploaded image directly into PDF file. Something like this:

PDF::load($data, 'A4', 'portrait')->inject('image', Input::file('resim'))->output();
//So I can reference it as {{ $image }} on PDF template, but image will be injected into PDF file

I'm beginner on PDF files and I'm not even sure if this is possible.

If it's possible, can anyone give me some advices?

Ps. I use DOMPDF package for Laravel 4. https://github.com/thujohn/pdf-l4

  • 写回答

2条回答 默认 最新

  • doulanyan6455 2013-10-04 19:48
    关注

    You will need to be able to reference the image somehow in your document. There are a few ways you could do this.

    Access the temporary file from the uploads.

    In your HTML file just reference the temporary file created by the upload as your image source, e.g.

    <img src="<?php echo $_FILES['userfile']['tmp_name']; ?>">
    

    Capture the content of the image and write it into the HTML as a data-uri value

    dompdf understands data-uris and so you could take the uploaded file, convert it to a data-uri, and insert that into your HTML.

    You do have to detect the image format, which can require a bit more work depending on your version of PHP. With v5.3+ you can use the finfo_file method. If that extension isn't available you can perform some basic content-type detection by parsing the file extension.

    In my example I'm just specifying PNG as the image type.

    <?php
    // detect mime type, but I'm forcing PNG
    $mime = 'image/png';
    ?>
    <img src="<?php echo 'data:' , $mime , ';base64,' , base64_encode( file_get_contents( $_FILES['userfile']['tmp_name'] ) ); ?>">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题