I am trying to convert an html file that contains the Arabic language to pdf and I used tcp pdf
public function generatePdf(){
$users = User::all();
$reportHtml = view('pdf.users', ['users' => $users])->render();
return PDF::HTML($reportHtml);
}
give me this error
"message": "Symfony\\Component\\HttpFoundation\\Response::setContent(): Argument #1 ($content) must be of type ?string, Barryvdh\\Snappy\\PdfWrapper given, called in C:\\Users\\amrel\\Desktop\\topArt\\vendor\\laravel\\framework\\src\\Illuminate\\Http\\Response.php on line 72",
You can't return pdf file directly. Try either
Option #1. Return response in application/pdf type:
return Response::make(PDF::HTML($reportHtml), 200, [
'content-type'=>'application/pdf',
]);
Option #2. From Laravel 5.2 you can return file in storage/ by just:
return response()->file($pathToFile);
So you can save it to your storage and then return.
Option #3. You can send your pdf as downloadable file.
$pdf = PDF::HTML($reportHtml);
return $pdf->download('name.pdf');
Option #4. You can save pdf file to public storage and send url link to it and let the frontend decide.
Related
I am trying to create a temporarily PDF file from raw PDF string.
This is my input, which is sent through an API (JSON):
{
"name": "pdffilename.pdf",
"content": "%PDF-1.2 [.......]%%EOF"
}
The "content" string is the actual PDF raw data string.
Now this is my controller, that handles the API request:
/**
* Function to convert a PDF file to text
*/
public function PDFtoText(Request $request)
{
$name = $request->name;
$content = $request->content;
//Save PDF file on the server (temp files).
$pdf = Storage::disk('local')->put('/temp_files/' . $name, $content);
return response()->json([
'result' => "Success"
], 200);
}
An actual file is created in the temp_files folder, with the name pdffilename.pdf. However I cannot open the file, as it says the file is "corrupt".
What am I doing wrong here?
the best way to save the content of the document or file is using the base64_encode function, this transform de content in base64 encode. Then when you need this content the unique process you need to do is to call the base64_decode function. I tried and works fine for me. Any questions only write me.
I use laravel dompdf to create PDF files. What I'm trying to do is create pdf file, upload it to storage and then download it. there's specific endpoint, when user call it all these actions happen.
// fetch data for pdf
$pdfData = $this->getData();
// creates pdf from html
$pdf = PDF::loadView('pdf.example', [
'items' => $pdfData
])->setPaper('a4', 'landscape');
// upload file to storage
Storage::put("/my-path", $pdf->output());
// download file
return $pdf->download("file.pdf");
In html(from which pdf's been created) I have custom fonts(load them with #font-face). the issue is that after calling output method $pdf->output() the characters in pdf are invalid
BUT, if I don't call $pdf->output() characters are shown properly(with my custom fonts). so the issue happens only when I use custom fonts and then call $pdf->output(). any idea how to fix it?
I already saw this solution but it doesn't help, also there's no load_font.php in this package
I am using the below code. but still it's downloading the files instead of viewing.
public function viewFiles(StoreBusinessDevelopment $request, $file, $id)
{
$businessDevelopment = BusinessDevelopment::select('created_by', 'rfp_id')
->where('id', '=', $id)
->get();
$mimeType = File::mimeType(public_path('/uploads/'.$businessDevelopment[0]['created_by'].'/'.$businessDevelopment[0]['rfp_id'].'/'.$file));
return response()->file(public_path('/uploads/'.$businessDevelopment[0]['created_by'].'/'.$businessDevelopment[0]['rfp_id'].'/'.$file),[
'Content-Type' => $mimeType
]);
}
You can just convert files to PDF format and then return it to browser.
Browser unable to read .doc|xls|xlsx files. But you can view / rander it by using some tricks.
Example 1
First upload the file like i say .doc|xls|xlsx and make it as array of data. By using php -> laravel that have a module/repo for excel file read and parse it as array.
After make it as array and print this array as html table and browser must rander it.
For more info :
Laravel Excel
Goal: When client requests to see a file from server, i want the client's browser to ask what to do (open or download file). How can i build that?
So far i have methods like those:
public static function getFileContent($fullpath){
//yes, i can get both file and its extension
$file = File::get($fullpath);
$mimeType = File::extension($fullpath);
$response = Response::make($file, 200, array('content-type'=>$mimeType));
return $response;
}
and the other method calls the above
$path = $instance->the_file_path_on_the_database;
return MyClass::getFileContent($path);
In this case, if the uploaded file is PDF, png, jpeg etc. browser automatically opens the file, which is okay.
But when it comes to the *.xlsx or *.docx, browser asks me what to do but the file name isn't as the same as what i have stored on the database and it has no extension. Also the file is automatically renamed as the route's name.
Thanks in advance.
according to the docs
The download method may be used to generate a response that forces the
user's browser to download the file at the given path. The download
method accepts a file name as the second argument to the method, which
will determine the file name that is seen by the user downloading the
file. Finally, you may pass an array of HTTP headers as the third
argument to the method:
return response()->download($pathToFile);
return response()->download($pathToFile, $name, $headers);
I am running Symfony 1.4 on Linux. My application creates pdf files and saves the files in the following directory:
/srv/www/vhosts/myapp/htdocs/stmts
Here is an example of the path to a particular pdf file:
/srv/www/vhosts/myapp/htdocs/stmts/example_001.pdf
My symfony is installed in the following path:
/srv/www/vhosts/myapp/htdocs
How can I create a route from my Symfony application to the example_001.pdf file? I want to be able to create a link in my symfony application to the pdf file. When a user clicks on the link the pdf will be opened.
Thank You
In order for using a route to make sense you would need to be doing something like this:
public function executeDownload(sfWebRequest $request)
{
// assume this method holds the logic for generating or getting a path to the pdf
$pdfPath = $this->getOrCreatePdf();
// disbale the layout
$this->setLayout(false);
$response = $this->getResponse();
// return the binary pdf dat directly int he response as if serving a static pdf file
$response->setHttpHeader('Content-Disposition', 'attachment; filename="'. basename($pdfPath));
$response->setContentType('application/pdf');
$response->setContent(file_get_contents($pdfPath));
return sfView::NONE;
}
That action would actually read the file and send the content. But unless you have a good reason for doing this its not advisable because youre going to incur unnecessary overhead from php.
If you do have a good reason for doin this (restricted access, dynamic file names, etc.) then you would simply determine what paramters you need to use in that action to determine the path to the pdf on the file system and set up a normal route. For example lets say your using a human recognizeable slug to refernece the file. Then you have a db record that holds a mapping of slug to file path. In that case the preceding action might look like this:
public function executeDownload(sfWebRequest $request)
{
$q = Doctrine_Core::getTable('PdfAsset')
->createQuery('p')
->where('slug = ?', $request->getSlug());
$this->forward404Unless($asset = $q->fetchOne());
$pdfPath = $asset->getPath();
// disbale the layout
$this->setLayout(false);
$response = $this->getResponse();
// return the binary pdf dat directly in the response as if serving a static pdf file
$response->setHttpHeader('Content-Disposition', 'attachment; filename="'. basename($pdfPath));
$response->setContentType('application/pdf');
$response->setContent(file_get_contents($pdfPath));
return sfView::NONE;
}
With the corresponding route looking something like:
pdf_asset:
url: /download/pdf/:slug
params: {module: yourModule, action: 'download'}
Note if the files are large you might want to use fopen instead of file_get_contents and then read the data out as a stream so that you dont have to put it all in memory. This would require you to use a view though (but you would still set layout to false to prevent the layout from wrapping your streamed data).