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

时间:2018-12-03 09:38:59

标签: 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]

我的树枝模板

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

注意:

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

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

1 个答案:

答案 0 :(得分:0)

我的问题是尝试使用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

它有效!谢谢您的帮助。