我需要在 pdf 文档中显示图像,但我的图像存储在公用文件夹之外。
我做了一个特定的动作来显示我的图像,它在前面工作正常,但我不能在我的 pdf 中显示我的图像。
我的行动:
/**
* @Route("/get_file/{uuid}", name="bo_client_header_get_file", methods={"GET"})
* @param $uuid
* @return Response|NotFoundHttpException
*/
public function getHeaderFile($uuid)
{
$uuid = Uuid::fromString($uuid);
$header = $this->getDoctrine()->getManager()->getRepository(Header::class)->findOneByUuid($uuid);
if ($header) {
$file = $this->projectDir . DIRECTORY_SEPARATOR . $this->documentsDir . $header->getFilepath();
if (!file_exists($file)) {
$file = $this->projectDir . DIRECTORY_SEPARATOR . 'public' . $header->getFilepath();
}
if (!file_exists($file)) {
$file = $this->projectDir . $header->getFilepath();
}
if (file_exists($file)) {
$response = new Response();
$response->headers->set('Content-Type', mime_content_type($file));
$response->setContent(file_get_contents($file));
return $response;
}
}
return new NotFoundHttpException('Header inexistant');
}
我的 html(树枝模板):
{{ url('bo_client_header_get_file', {client: announce.header.client.id, uuid: announce.header.uuid}) }}
<div class="hg-headercard">
<img class="announceHeaderimg"
height="{{ announce.header.size }}" src="{{ url('bo_client_header_get_file', {client: announce.header.client.id, uuid: announce.header.uuid}) }}"/>
第一个 url() 用于调试目的。打印在我的 pdf 上的 url 是有效的,如果我将其粘贴到浏览器中,则会显示我的图像,所以我担心我的问题在于某些特定的 wkhtmltopdf 设置。也许我在控制器中缺少一些响应头。
我正在使用 symfony 5.3、wkhtmltopdf 0.12.6 和修补过的 qt