I am using Sonata Media Bundle to let users upload PDF files. I want to use Imagick to create a preview image of a pdf document. I have a $media
object that holds information about my pdf.
When I do die(dump($media))
, I see a media object that includes the following line:
#providerReference: "3c4460aae99b0084d08252065bf3eea1817842d9.pdf"
... and opening that file via:
open -a Preview ./data/storage/faq/0001/01/3c4460aae99b0084d08252065bf3eea1817842d9.pdf
... on my mac yields a fully legible PDF.
The problem is that I also see the following line in the browser:
#binaryContent: null
... which means that I can't dump the binary content into a temporary file for the purpose of having Imagick manipulate it.
How do I get a file path that I can hand off to Imagick?
Here is roughly what worked in order to get me a useful reference to a file, complete with valid binary content.
public function preUpdate(Document $document)
{
$media = $document->getMedia();
if ($media === null) {
return;
}
$mediaName = $media->getName();
if ($media->getContentType() !== 'application/pdf') {
return;
}
$context = $media->getContext();
$formats = $pool->getFormatNamesByContext($context);
if (null === $formats) {
return;
}
$provider = $this->getProvider($media->getProviderName());
$publicUrls = [];
$fullFilePath = $provider->getReferenceImage($media);
}
...
private function getPool()
{
return $this->container->get('sonata.media.pool');
}
...
private function getProvider($name)
{
return $this->container->get($name);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.