Symfony Base64_encode on pdf in response - symfony

I want to base64_encode a pdf file before returning it to the client.
Here is what I do
$response = $event->getResponse();
$response->headers->remove('Content-Disposition');
$response->setContent(
$response->headers->get('Content-Type')
. ';base64,'
. base64_encode($response->getContent())
);
$response->headers->set('Content-Type', 'text/plain');
The pdf that I get in a browser when I put data:<base64_encoded_string> doesn't have any value, but the whole skeleton/css is ok.
If I do
$response = $event->getResponse();
$response->headers->remove('Content-Disposition');
$response->headers->set('Content-Type', 'application/pdf');
I do get a valid pdf file with all the values.
Is it possible that the base64_encoding is breaking something ?
Thanks

I found the answer, I had a sub-request, so its content was also base64 encoded, that's why the master request's content was broken.

Related

How to return binary data from custom wordpress rest api endpoint

I am writing a custom endpoint for a REST api in wordpress, following the guide here: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
I am able to write a endpoint that returns json data. But how can I write an endpoint that returns binary data (pdf, png, and similar)?
My restpoint function returns a WP_REST_Response (or WP_Error in case of error).
But I do not see what I should return if I want to responde with binary data.
Late to the party, but I feel the accepted answer does not really answer the question, and Google found this question when I searched for the same solution, so here is how I eventually solved the same problem (i.e. avoiding to use WP_REST_Response and killing the PHP script before WP tried to send anything else other than my binary data).
function download(WP_REST_Request $request) {
$dir = $request->get_param("dir");
// The following is for security, but my implementation is out
// of scope for this answer. You should either skip this line if
// you trust your client, or implement it the way you need it.
$dir = sanitize_path($dir);
$file = $request->get_param("file");
// See above...
$file = sanitize_path($file);
$sandbox = "/some/path/with/shared/files";
// full path to the file
$path = $sandbox.$dir.$file;
$name = basename($path);
// get the file mime type
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $path);
// tell the browser what it's about to receive
header("Content-Disposition: attachment; filename=$name;");
header("Content-Type: $mime_type");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($path));
header("Cache-Control: no-cache private");
// stream the file without loading it into RAM completely
$fp = fopen($path, 'rb');
fpassthru($fp);
// kill WP
exit;
}
I would look at something called DOMPDF. In short, it streams any HTML DOM straight to the browser.
We use it to generate live copies of invoices straight from the woo admin, generate brochures based on $wp_query results etc. Anything that can be rendered by a browser can be streamed via DOMPDF.

Symfony2 PHPWord response

I am trying to generate a docx document on Symfony2, using the PHPWord bundle.
In my controller, I succeed in returning a docx file, but it is empty, I think it comes from my faulty response format.
public function indexAction($id)
{
$PHPWord = new PHPWord();
$section = $PHPWord->addSection();
$section->addText(htmlspecialchars(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
));
// Saving the document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
return new Response($objWriter->save('helloWorld.docx'), 200, array('Content-Type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'));
}
Try this class
<?php
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use Symfony\Component\HttpFoundation\Response;
class WordResponse extends Response
{
/**
* WordResponse constructor.
* #param string $name The name of the word file
* #param PhpWord $word
*/
public function __construct($name, &$word)
{
parent::__construct();
// Set default zip library.
if( !class_exists('ZipArchive')){
Settings::setZipClass(Settings::PCLZIP);
}
$writer = IOFactory::createWriter($word, 'Word2007');
//Set headers.
$this->headers->set("Content-Disposition", 'attachment; filename="' . $name . '"');
$this->headers->set("Content-Type", 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$this->headers->set("Content-Transfer-Encoding", 'binary');
$this->headers->set("Cache-Control", 'must-revalidate, post-check=0, pre-check=0');
$this->headers->set("Expires", '0');
$this->sendHeaders();
$writer->save('php://output');
}
}
Then in your controller do:
return new WordResponse($phpWord, "filename.docx");
Thanks a lot for your answer.
I achieve using the 2nd method, which is in my opinion the best.
I just have to return a response, otherwise the file was generated, but stuck in the web directory.
Using this response, everything was fine and a download prompt appeared, with the "full" file.
Here's my code :
$PHPWord = new PHPWord();
$section = $PHPWord->addSection();
$section->addText(htmlspecialchars(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
));
// Saving the document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$filename="MyAwesomeFile.docx";
$objWriter->save($filename, 'Word2007', true);
$path = $this->get('kernel')->getRootDir(). "/../web/" . $filename;
$content = file_get_contents($path);
$response = new Response();
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$response->headers->set('Content-Disposition', 'attachment;filename="'.$filename);
$response->setContent($content);
return $response;
PHPWord->save() returns a true value so that would be why your file is not being downloaded. With your return new Response() you are setting the content of your response to true (the result of your save call) which is why your response is empty.
You have 2 (and probably more that I haven't thought of) options to generate and download this file..
1. Save your file to a temp folder and server from there
$filename = sprintf(
'%s%sDoc-Storage%s%s.%s',
sys_get_temp_dir(),
DIRECTORY_SEPARATOR,
DIRECTORY_SEPARATOR,
uniqid(),
'docx'
);
$objWriter->save($filename);
$response = new BinaryFileResponse($filename);
For more info on the BinaryFileResponse see the docs.
2. Ignore Symfony and serve directly via the PHPWord action
$objWriter->save($filename, 'Word2007', true);
exit();
The ->save method provides all of the actions to download the generated file internally (see the code) so all you need to do is set the format and the third parameter to true and it will handle all of the headers for you. Granted it won't be returning a Symfony response but you will be exiting out before you get to that exception.

Symfony Audio Stream with Gaufrette

I am using KnpGaufretteBundle to store audio files. I am able to download a given file to the client like this:
$filename = "Somefilename.mp3";
$fs = $this->filesystemMap->get('media_fs');
$file = $fs->read($filename);
if($file){
//Create And Return Response
$response = new Response();
$disp = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$variant->getFileName()
);
$response->headers->set('Content-Length', $fs->size($filename));
$response->headers->set('Accept-Ranges', 'bytes');
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Content-Type', 'application/octet-stream');
$response->headers->set('Content-Disposition', $disp);
$response->setContent($file);
return $response;
}
But now I also want to stream the file to the client, instead of using the attachment content disposition. Basically I want to access it clientside as if I was pointing at an actual mp3 sitting on my server. Does anyone know how this can be done?
I solved this by using the streamwrapper... it was this easy.
$filepath = 'gaufrette://myFileSystemName/'.$filename;
$response = new \Symfony\Component\HttpFoundation\BinaryFileResponse($filepath);

Show pdf in symfony2

I want to show a pdf, but I did this and I only gets to download the pdf: This is my code:
$response = new BinaryFileResponse($path);
$response->trustXSendfileTypeHeader();
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $file . '.pdf');
return $response;
Any idea?
Have you tried setting Content-Type header?
$response->headers->set('Content-Type', 'application/pdf');
Also, ditch that setContentDisposition call since DISPOSITION_ATTACHMENT value forces your browser to download the file.

How to retrieve a streamed response (e.g. download a file) with Symfony test client

I am writing functional tests with Symfony2.
I have a controller that calls a getImage() function which streams an image file as follows:
public function getImage($filePath)
$response = new StreamedResponse();
$response->headers->set('Content-Type', 'image/png');
$response->setCallback(function () use ($filePath) {
$bytes = #readfile(filePath);
if ($bytes === false || $bytes <= 0)
throw new NotFoundHttpException();
});
return $response;
}
In functional testing, I try to request the content with the Symfony test client as follows:
$client = static::createClient();
$client->request('GET', $url);
$content = $client->getResponse()->getContent();
The problem is that $content is empty, I guess because the response is generated as soon as the HTTP headers are received by the client, without waiting for a data stream to be delivered.
Is there a way to catch the content of the streamed response while still using $client->request() (or even some other function) to send the request to the server?
The return value of sendContent (rather than getContent) is the callback that you've set. getContent actually just returns false in Symfony2
Using sendContent you can enable the output buffer and assign the content to that for your tests, like so:
$client = static::createClient();
$client->request('GET', $url);
// Enable the output buffer
ob_start();
// Send the response to the output buffer
$client->getResponse()->sendContent();
// Get the contents of the output buffer
$content = ob_get_contents();
// Clean the output buffer and end it
ob_end_clean();
You can read more on the output buffer here
The API for StreamResponse is here
For me didn't work like that. Instead, I used ob_start() before making the request, and after the request i used $content = ob_get_clean() and made asserts on that content.
In test:
// Enable the output buffer
ob_start();
$this->client->request(
'GET',
'$url',
array(),
array(),
array('CONTENT_TYPE' => 'application/json')
);
// Get the output buffer and clean it
$content = ob_get_clean();
$this->assertEquals('my response content', $content);
Maybe this was because my response is a csv file.
In controller:
$response->headers->set('Content-Type', 'text/csv; charset=utf-8');
The current best answer used to work well for me for some time, but for some reason it isn't anymore. The response is parsed into a DOM crawler and the binary data is lost.
I could fix that by using the internal response. Here's the git patch of my changes[1]:
- ob_start();
$this->request('GET', $uri);
- $responseData = ob_get_clean();
+ $responseData = self::$client->getInternalResponse()->getContent();
I hope this can help someone.
[1]: you just need access to the client, which is a
Symfony\Bundle\FrameworkBundle\KernelBrowser

Resources