symfony - Html2Pdf + Symfony + Docker:无法获取图像的大小

标签 symfony docker html2pdf

我用HMTL2Pdf生成Pdf时遇到问题。
我用 :

  • Symfony框架
  • spipu / html2Pdf
  • Docker,其中包含用于应用程序的容器(8080)和用于文件
  • 的Minio容器(9001)

    我尝试在pdf上显示图像,但出现错误:Unable to get the size of the image [http://127.0.0.1:9001/events/photos/37652647-203b-4196-8792-6b5da989eddc.jpeg]

    我的 Twig 模板
    {% for page in pages %}
    <page backtop="20mm" backright="10mm" backbottom="20mm" backleft="10mm">
        <img src="{{ page.background|storage_url }}">
    </page
    {% endfor %}
    

    我的 Controller
    public function handle(): string
    {
        $template = $this->renderer->render('pdf/index.html.twig', [
            'pages' => $this->pageManager->findAll(),
        ]);
    
        $this->pdfCreator->create('L', 'A4', 'fr', true, 'UTF-8', array(10, 15, 10, 15));
        return $this->pdfCreator->generatePdf($template, "test");
    }
    

    我的服务pdfCreator
    use Spipu\Html2Pdf\Html2Pdf as Html2Pdf;
    
    final class PdfCreator implements PdfCreatorInterface
    {
        /** @var Html2Pdf $pdf */
        private $pdf;
    
        public function create($orientation = null, $format = null, $lang = null, $unicode = null, $encoding = null, $margin = null): void
        {
            $this->pdf = new Html2Pdf($orientation, $format, $lang, $unicode, $encoding, $margin);
        }
    
        /**
         * @param $template
         * @param $name
         * @return string
         * @throws \Spipu\Html2Pdf\Exception\Html2PdfException
         */
        public function generatePdf($template, $name): string
        {
            $this->pdf->writeHTML($template);
            return $this->pdf->Output($name.'.pdf');
        }
    }
    

    注意 :
  • 如果我删除<img>,我的PDF将正确显示
  • 我的图像路径是正确的,因为我可以在应用程序
  • 的不同页面上以相同的方式显示它

    你能帮我展示我的形象吗?我无法使用绝对路径,因为我的图片来自其他容器Minio(9001),所以我不知道该怎么办

    最佳答案

    我的问题是尝试使用9001 URL访问我的图像,所以我这样做解决了我的问题:

  • 获取我的图像的内容
  • 创建具有该内容的临时文件
  • 在pdf
  • 中显示此临时文件

    我的范本
    {% for page in pages %}
        <page backtop="20mm" backright="10mm" backbottom="20mm" backleft="10mm">
            <img src="/tmp/{{ page.background }}" style="width: 100px; height: 100px">
        </page>
    {% endfor %}
    

    我的 Controller
    public function handle(FileUploader $fileUploader): string
    {
        $pages = $this->pageManager->findAll();
        foreach ($pages as $page) {
            $fileUploader->makeTempFile($page->getBackground());
        }
        $template = $this->renderer->render('pdf/index.html.twig', [
            'pages' => $pages,
        ]);
    
        $this->pdfCreator->create('L', 'A4', 'fr', true, 'UTF-8', array(10, 15, 10, 15));
        return $this->pdfCreator->generatePdf($template, "test");
    }
    

    我的服务FileUploader
    use Gaufrette\FilesystemInterface;
    use Ramsey\Uuid\Uuid;
    use Symfony\Component\HttpFoundation\File\File;
    
    final class FileUploader implements FileUploaderInterface
    {
        private $filesystem;
    
        public function __construct(FilesystemInterface $filesystem)
        {
            $this->filesystem = $filesystem;
        }
    
        public function upload(File $file): string
        {
            $filename = Uuid::uuid4()->toString().'.'.$file->guessExtension();
    
            $this->filesystem->write($filename, file_get_contents($file->getPathname()));
    
            return $filename;
        }
    
        public function get(string $fileName)
        {
            return $this->filesystem->read($fileName);
        }
    
        public function makeTempFile(string $fileName)
        {
            $content = $this->get($fileName);
            file_put_contents(sys_get_temp_dir().'/'.$fileName, $content);
        }
    }
    

    我将GD添加到我的DockerFile php-fpm中
    FROM php:7.2-fpm
    
    RUN apt-get update
    
    RUN apt-get install -y libpq-dev git libicu-dev libpng-dev libjpeg62-turbo-dev \
        && docker-php-ext-configure intl \
        && docker-php-ext-install intl pdo pdo_pgsql pgsql gd
    
    WORKDIR /var/www/symfony
    

    而且有效!谢谢你的帮助。

    关于symfony - Html2Pdf + Symfony + Docker:无法获取图像的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53590956/

    相关文章:

    docker - 通过 Docker-Compose 为 ElasticSearch 使用持久主机卷

    javascript - 如何使用 Nodemailer 附件的相对路径?

    php - UndefinedFunctionException 试图从命名空间 xxx 调用函数 xxx

    docker - 如何使用 docker-compose.yml 和 ecs-cli 将 EBS 卷附加到我的容器

    windows - 在 Windows 上使用 virtualbox 的 docker

    php - 如何使用 HTML2PDF 设置横向

    symfony - PhpStorm Xdebug Symfony 配置

    mysql - 在 Symfony2 QueryBuilder 中使用计数和分组的 SQL 查询

    php - 在 Twig 中解码 JSON