I'm facing two problems.
force_download() does not work for pdf extensions.
when I try to download a txt extension file the functions work fine
below code.
public function index()
{
$data = "some text";
$name = "sample.txt";
force_download($name,$data);
}
but when I try to do it with a pdf extension, the file gets downloaded properly but when I click the downloaded pdf file it shows Error: Failed to load PDF document.
public function index()
{
$data = "some text for pdf";
$name = "sample.pdf";
force_download($name,$data);
}
secondly is there a way to ask or show a dialogue box before downloading a file instead of force download.
You cannot create a (not currupt) pdf file with force_download(). You'll need to create the pdf file using a php library like fpdf or similar. The force_download() function only generates a header which forces a download to happen.
If the pdf file exists on the server you can force_download('/path/to/my_pdf.pdf', NULL);
concerning the second part of your question, this is too broad, there are thousands of ways to create a dialog box
pass name of the file you want to download
Controller
function download($file_name)
{
$this->load->helper('download');
force_download('./assets/'.$file_name, NULL);
}
View
<a href="<?php echo SURL.'controller_name/download/'.$file_name;?>">Download
</a>
For second question try onclick()
Related
I have a strange question regarding PDF.JS and PHP
Using PDF.JS, to open pdf you should use on browser:
http:// ...url... /viewer.html?file=[filename]
I have changed viewer.html extension to viewer.php and developed an additional file document.php to get data from database and load a local PDF file, and I use in this way to get PDF
... viewer.php?file=document.php&control=5E71581C52B96
All work great , AS ESPECTED, but i'm experiencing trouble when get variables from database.
When I Use fileurl like this, PDF load correctly:
$fileurl = 'D:\Drive\_DEV\storage\220\2020-03-17\00000000002\1584486427900.pdf';
When I use fileurl like this (variables from database) PDF not open and have error
Corrupted or inválid PDF Invalid PDF structure
$fileurl = $storage_path.'\\'.$storage_folder.'\\'.$document;
If I echo, $fileurl in both cases i have exactly same result.
Regarding SQL QUERY below, on WHERE Clause if I change '$doc_control' (from request) with '5E71581C52B96' (instead '$doc_control') PDF load correctly into pdf.js
If I echo $doc_control, number is exactly same, only difference is how put value on WHERE (number or variable)
If i open document.php work with no problems.
What do I Wrong? Any help is very appreciated.
DOCUMENT.PHP
// REQUEST
$doc_control = $_REQUEST['control'];
// Read database
SELECT *
FROM docs
WHERE doc_control = '$doc_control'
// FILE PATH
$fileurl = $storage_path.'\\'.$storage_folder.'\\'.$document;
// READ PDF
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename=".$fileurl);
//#readfile($fileurl);
$file=fopen($fileurl, "r") or die('Unable to open file');
echo fread($file,filesize($fileurl));
fclose($file);
Expected requirement - when user clicks button ajax will be call to controller where pdf will generate and generated pdf will gets downloaded on user's system.
Actual Scene - i able to create pdf using fpdf file and saved in folder and used force download function but nothing happend.
here's my code:
Controller :
$this->load->helper('download');
$this->load->library('myfpdf');
$this->load->view('pdf/namewise_report',[]);
$link = base_url().'uploads/pdfs/namewise.pdf';
force_download($link, NULL);
pdf/namewise_report.php (last line)
$this->myfpdf->Output('uploads/pdfs/namewise.pdf', "F");
is there anything to add in ajax success response
I have some problems how to show PDF in Laravel.
What I did is:
web.php
Route::get('/datenschutz', function() {
$pdf = \PDF::loadView('pdf.Datenschutzerklarung');
return $pdf->stream('Datenschutzerklarung.pdf');
} )->name('getDatenschutzerklärung');
And I have that file in that folder, it loads great, but problem is, file is empty!
How I can post as HTML that pdf that I have?
I guess I have to convert that file in HTML, but I can't include third part 'pdf converters'.
I just create a simple download file function by :
public function actionDownload(){
$id = Yii::$app->request->getQueryParam('id');
// $id = Yii::$app->request->post('file_id');
$path =( new DocumentCRUD())->getDocumentPath($id);
$response = Yii::$app->response->sendFile($this->ROOT_FOLDER.'/'.$path);
$response->send();
}
and in view ,file will be downloaded when click link
window.location.href="document/download?id="+file_id
The problem is that ,after file downloaded ,I can not open binary file such : image ,exe ... just text file is OK ,and im sure that these file on server is no problem
What I have to do ?
I found my problem,just clear all dummy output before send file by ob_end()
I use the FPDF library to display online PDF files, with the ouput option « I » (which output the content to browser).
Then, I need to be able to save some of those generated pdfs on my server (like does the ouput option « F »).
In order to do that, I wrote this script:
$pdf_data = file_get_contents('https:/HOST/Folder/file.php');
$path =__DIR__.'/Folder/file.pdf';
file_put_contents( $path, $pdf_data );
It succeeds to create a file with pdf extension to the directory, but :
- I got an error: « Format error : not a pdf or corrupted » when I try to open the "pdf" file,
- The saved file is empty (?).
So, what is wrong?
How can I get the content generated by the php file, and save it as a pdf on my server?
Finaly, I forgot this script and found a new solution that works!
I set up two outputs into my PHP code build with FPDF, one to display the file online and one to save the file on the server.
The content generated by the PHP code is always displayed, and I control the PDF backup with a string on the first output.
So, when I need to backup, I use a Jquery function to execute the PHP code in the background and upload the PDF file (hidden iframe works too).
Below the end of my PHP code:
<?php
...
if (mycondition) {$string='F';}
else {$string ='I';}
...
$pdf->Output(__DIR__.'/../../Folder/file.pdf',$string); // save on the server
$pdf->Output('file.pdf','I'); // display on the browser
?>
Below the jQuery function I use:
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript">
var phpfile = <?php echo json_encode($Filename); ?>; // the php file to upload
function uploadpdf() {
$.get(phpfile);
return false; }
</script>
Hope this will help!