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

Html2Pdf + Symfony + Docker : Unable to get the size of the image

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

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

我的树枝模板

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

我的控制器

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');
    }
}

注:

你能帮我展示一下我的形象吗?我不能使用绝对路径,因为我的图像来自另一个容器 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 %}

我的控制器

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

而且有效!感谢您的帮助。