简体   繁体   English

如何使用 Sonata Media Bundle 保存 pdf 文件

[英]How to save pdf files with Sonata Media Bundle

I tried to save my pdf files into the file system of sonata media bundle and insert the correct database entry.我试图将我的 pdf 文件保存到奏鸣曲媒体包的文件系统中,并插入正确的数据库条目。

I created my file with dompdf as following:我用 dompdf 创建了我的文件,如下所示:

$dompdf = new Dompdf($pdfOptions);
$html = $this->renderView("pdf/hrprotokoll.html.twig", [
      'result' => $result,
      'vals' => $vals,
      'title' => 'Prüfprotokoll',
      'item' => $item,
      ]);

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$output = $dompdf->output();
$publicDirectory = $request->server->get('DOCUMENT_ROOT') . '/uploads/hrprotokolle/'.$this->getUser()->getStandort()->getId();
$pdfFilepath =  $publicDirectory . '/'. time().'.pdf';
$file = time().'.pdf';
file_put_contents($pdfFilepath, $output);

ok, now the file is saved correctly without errors.好的,现在文件已正确保存,没有错误。 The question now is how to manage this file with Sonata MediaBundle.现在的问题是如何使用 Sonata MediaBundle 管理这个文件。 Ive tried the following:我试过以下方法:

$mediaManager = new MediaManager();
$media = new Media();
$media->setBinaryContent($file);
$media->setContext('default');
$media->setProviderName('sonata.media.provider.file');
$mediaManager->save($media);

Error:错误:

Too few arguments to function Sonata\Doctrine\Model\BaseManager::__construct(), 0 passed... ($mediaManager)

Im not able to set the correct way.我无法设置正确的方式。 Anyone have an idea to solve this problem?有人有解决这个问题的想法吗? Thx谢谢

The problem was: $mediaManager (service sonata.media.manager.media) cant autowire so you have to do this manually.问题是:$mediaManager (service sonata.media.manager.media) 不能自动装配,所以您必须手动执行此操作。

at first check this service in your terminal and find informations about this service by typing the following:首先在您的终端中检查此服务,并通过键入以下内容查找有关此服务的信息:

 php bin/console debug:container sonata.media.manager.media

The Result is:结果是:

Service ID       sonata.media.manager.media
Class            Sonata\MediaBundle\Entity\MediaManager

Ok, now go to your services.yaml and create the alias:好的,现在 go 到您的 services.yaml 并创建别名:

Sonata\MediaBundle\Entity\MediaManager: '@sonata.media.manager.media' 

Its done.完成。 Now you can inject the MediaManager and it works for me:现在您可以注入 MediaManager,它对我有用:

use Sonata\MediaBundle\Entity\MediaManager;

private $mediaManager;

public function __construct(MediaManager $mediaManager)
{
    $this->mediaManager = $mediaManager;
}

$media = new Media(); 
$this->mediaManager->save($media);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM