How to save pdf files with Sonata Media Bundle - php

I tried to save my pdf files into the file system of sonata media bundle and insert the correct database entry.
I created my file with dompdf as following:
$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.
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.
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:
Sonata\MediaBundle\Entity\MediaManager: '#sonata.media.manager.media'
Its done. Now you can inject the MediaManager and it works for me:
use Sonata\MediaBundle\Entity\MediaManager;
private $mediaManager;
public function __construct(MediaManager $mediaManager)
{
$this->mediaManager = $mediaManager;
}
$media = new Media();
$this->mediaManager->save($media);

Related

Google Storage - dynamically change ACL on single object (PHP)

I need to update the ACL basically by adding or removing the allUsers entity.
I have the PHP library and what I'm doing at moment is:
$storage = new StorageClient([
'projectId' => "xxxxx",
'keyFilePath' => mykey,
]);
$bucket = $storage->bucket('mybucket');
$acl = $bucket->acl('objectAccessControls', 'path/file/on/bucket');
if(add){
$acl->add('allUsers', 'READER');
}else{
$acl->delete('allUsers');
}
With this code actually changes ALL bucket configuration, not the file only.
How can I correctly specify the path of a specific file and change permissions only on path/file/on/bucket? I'm using the wrong functions?
Here the documentation that I'm using
https://googleapis.github.io/google-cloud-php/#/docs/google-cloud/v0.90.0/storage/acl
This is the case if add:
This is the else case:
UPDATE 1:
Using this to delete seems working -> https://cloud.google.com/storage/docs/json_api/v1/objectAccessControls/delete
Tried to include the parameters listed here to the call I do, something like this:
$options = ['object' => 'path/obj'];
$acl->delete('allUsers', $options)
Still not working
Actually I've solved by using the Google_Service
$client = new Google_Client();
$client->setApplicationName('GoogleBuck/0.1');
$client->useApplicationDefaultCredentials(); // app engine env
$client->addScope('https://www.googleapis.com/auth/devstorage.full_control');
$storage = new Google_Service_Storage($client);
$acl = new Google_Service_Storage_ObjectAccessControl($client);
$acl->setEntity('allUsers');
$acl->setRole('READER');
$acl->setBucket($bucketName);
$acl->setObject($objectName);
To add
$response = $storage->objectAccessControls->insert($bucketName, $objectName, $acl);
To delete
$response = $storage->objectAccessControls->delete($bucketName, $objectName, 'allUsers');

Blueimp jQuery fileupload Plugin and Symfony : how to dynamically change file repository name based on an id?

I use the plugin 'blueimp jquery fileupload' to upload files in Javascript (that part I got it right) and then I have to handle the uploads on the server side using an uploadhandler affiliated to the plugin (UploadHandler.php).
Working in Symfony, I managed to create a service based on this php script and it works in my controller (the files are uploaded in the default repository, yet on my page I get the error message 'upload failed' and I don't know why but it's not a big problem I guess), but the thing is :
I would like to custom the repository path to upload the files based on the user id, and since I call the uploadhandler file as a service, I don't know how to override the options using the construct function, as I would be able to with a basic call in php.
Here's my code :
public function uploadFiles(Request $request)
{
$uploadhandler = $this->container->get('extranetcontratbundle.uploadhandler');
$response = $uploadhandler->response;
$files = $response['files'];
return new JsonResponse($files);
}
In the options of UploadHandler.php there is :
$this->options = array(
'script_url' => $this->get_full_url().'/'.$this->basename($this->get_server_var('SCRIPT_NAME')),
'upload_dir' => dirname($this->get_server_var('SCRIPT_FILENAME')).'/files/',
'upload_url' => $this->get_full_url().'/files/',
'input_stream' => 'php://input',
'user_dirs' => false,
'mkdir_mode' => 0755,
'param_name' => 'files',
...blabla
And I would like to override the options in a similar way as I would in 'normal' php :
$tmpImagesDir = JPATH_ROOT . 'tmp' . $userId .;
$tmpUrl = 'tmp/' . $userId . '/' . '/';
$uploadOptions = array('upload_dir' => $tmpImagesDir, 'upload_url' => $tmpUrl);
$uploadHandler = new UploadHandler($uploadOptions);
But to do that I would have to write "require_once(blabla)" and I would have created the service for nothing. If I understood it right, that's not the way to do it in Symfony. Is there a way ?
Thank you for reading, please help.

Upload PDF file on Google Cloud Storage with mpdf library

I am using mpdf library to convert HTML to PDF and successfully stored my pdf file on local as well as remote server. But I don't want to store my pdf files on my code repos on server and like to utilize storage bucket available on google cloud.
/*
*/
private function generatePDF($params, $quotationId) {
$location = '/var/www/html/development/pdfs/';
$html = $this->load->view('quotation', $data, TRUE);
$filename = "quo_" .time() . ".pdf";
$mpdf = new \Mpdf\Mpdf(['mode' => 'en-IN', 'format' => 'A4']);
$mpdf->WriteHTML($html);
$mpdf->SetHTMLFooter('<p style="text-align: center; text-size: 12px;">This is computer generated quotation. It does not require signature.</p>');
$pdf = $mpdf->Output($location . $filename, 'F');
$this->UploadModel->upload($pdf, $filename);
}
public function upload($pdf, $pdfName) {
$storage = new StorageClient();
$bucket = $storage->bucket("bucketname");
$object = $bucket->upload($pdf, ['name' => $pdfName]);
$object = $bucket->object($pdfName);
$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']);
}
Here I have used 'F' type in which it saves the pdf file in pdfs folder created in my code repo hosted on cloud server but I would like to directly store it to Google cloud storage bucket.
I am not having much experience about google cloud and mpdf library so looking for help and guidance to achieve the functionality.
Please kindly help me.
I see you are using Cloud Storage Client Libraries for PHP.
First, you need to install it to your machine:
composer require google/cloud-storage
And then you need to set up authentication by following the guide.
Once these are set create a bucket to store the PDFs.
Then replace your upload function with the code from the documentation:
use Google\Cloud\Storage\StorageClient;
/**
* Upload a file.
*
* #param string $bucketName the name of your Google Cloud bucket.
* #param string $objectName the name of the object.
* #param string $source the path to the file to upload.
*
* #return Psr\Http\Message\StreamInterface
*/
function upload_object($bucketName, $objectName, $source)
{
$storage = new StorageClient();
$file = fopen($source, 'r');
$bucket = $storage->bucket($bucketName);
$object = $bucket->upload($file, [
'name' => $objectName
]);
printf('Uploaded %s to gs://%s/%s' . PHP_EOL, basename($source), $bucketName, $objectName);
}
i also faced same issue & came out with this solution, i hope it will help you.
use 'S' instead of 'F'parameter, so it will return string data & pass this data directly into upload method.

How to download a file with RESTFUL cakePHP 2.3

I am using CakePHP 2.3 and I have two apps on 2 different servers. I am required to download a file from the first server using REST. I have made my applications RestFul and have configured the routes. I am able to post, get, put and delete but I cannot get it done to download the file.
Below is a sample code for a get
public function view($id) {
$object = $this->Imodel->find('first', array('conditions' => array('Imodel.id' => $id), 'contain' => array()));
$this->set(array(
'object' => $object,
'_serialize' => array('object')
));
}
I would appreciate any help to download a file with REST, complying with the Restful architecture that I already have in place.
Edit
After some time, I finally got it to work. In case someone else runs into the same problem, the whole thing was about understanding cakePHP HttpSocket better.
So first on the server where the webservice is registered (where we download the file from), below is my function; its response is a file as explained (here)
public function getpdffile($id = NULL){
$filepath = APP. 'Files/file.pdf'; //path to the file of interest
$this->response->file($filepath);
return $this->response;
}
Since the file was not public (not in webroot), i had to use MediaView. Then after setting this, I'd retrieve it for download using HttpSocket as shown below:
public function download($id = NULL, $fileMine = 'pdf', $fileName = 'file', $download = TRUE){
$httpSocket = new HttpSocket();
$filepath = APP. 'Files/myfile.pdf';
$file = fopen($filepath, 'w');
$httpSocket->setContentResource($file);
$link = MAIN_SERVER."rest_models/getpdffile/".$id.".json";
$httpSocket->get($link);
fclose($file);
$this->response->file($filepath);
return $this->response;
}
What I did there was copy the file to the App folder of my server and render it in a view.
I hope it helps someone :-)
On the server which call the file to download :
$file = file_get_contents(urlwhereyoudownload) ;
And on the server where webservice is register :
header('Content-type: $mimetypeoffile');
header('Content-Disposition: attachment; filename=".$fileName."');
readfile("$pathtofile");exit;

PhpRenderer not rendering my view script from console route

I've created a Console route on my zf2 application to dispatch an email via the command line. So in my action I am creating a new PhpRenderer as specified in the documentation (http://framework.zend.com/manual/2.0/en/modules/zend.view.renderer.php-renderer.html) to render my email template (replace variables etc) and dispatch it.
Here is the code I am using:
$renderer = new \Zend\View\Renderer\PhpRenderer();
$resolver = new \Zend\View\Resolver\TemplateMapResolver();
$resolver->setMap(array(
'mailTemplate' => $config['template']
));
$renderer->setResolver($resolver);
$model = new \Zend\View\Model\ViewModel();
$model->setTemplate('mailTemplate');
$model->setVariables(array(
'recipient' => 'foo#bar.com'
));
$emailBody = $renderer->render($model);
However it seems that the Phprenderer does not render any PHP in my .phtml file. The exact same code works correctly however if I execute it in a normal HTTP Request.
Could anyone help me out with this?
I suspect that it's because you haven't set the correct path to the resolver.
For reference, this is the approach I took:
// render HTML and TEXT bodies
$basePath = realpath(__DIR__ . '/../../../view/emails');
$htmlFilename = 'body.html.phtml';
$txtFilename = 'body.txt.phtml';
$renderer = new PhpRenderer();
$renderer->resolver()->addPath($basePath);
$sm = $this->getServiceManager();
$renderer->setHelperPluginManager($sm->get('ViewHelperManager'));
$model = new ViewModel();
$model->setVariable('name', $user->getName());
$model->setTemplate($txtFilename);
$textContent = $renderer->render($model);
$model->setTemplate($htmlFilename);
$htmlContent = $renderer->render($model);

Categories