multiple pdf download using zend framework - zend-framework

i got this code in attaching a pdf file to my site in order for the viewers to download such pdf file..
$fullPath = "../public/pdffiles/FolioPlusUserGuide(v3.0).pdf";
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
html code:
baseUrl().'/DownloadPdf'?>">DOWNLOAD BROCHURE
this code only accommodates 1 file path, I have more pdf files with different filename and different buttons for each to handle the download event. how can i achieve this?..thanks ahead!..=)

You can pass PDF file name via HTTP request, like this:
DownloadPDF.php?filename=test1.pdf
Then in your DownloadPDF file have something like this:
$pdfdir = "/pdffiles/folder/location/here";
$pdffilename = $_GET['filename'];
$fullPath = $pdfdir.$pdffilename;
if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
header("Content-length: $fsize");
header("Cache-control: private");
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
Now your code is dynamic and you can download any PDF with links like this:
<a href=downloadpdf.php?filename=something.pdf>Something</a>
<a href=downloadpdf.php?filename=something2.pdf>Something2</a>
etc.

Related

How to create docx file and download it using PhpWord

Hi I always got error when trying to create docx file and I want to download it directly. My browser is stuck on ERR_INVALID_RESPONSE. I'm using CodeIgniter framework by the way. Here is the code.
<?php
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\Word2007;
class Test extends MY_Controller{
public function create()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$file = 'HelloWorld.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
}
}
This code is just taken from the example in the PhpWord docs, but somehow it doesn't work. No error just ERR_INVALID_RESPONSE on the browser. What did I miss?
After a couple hours digging, I finally found the solution for my problem. The problem exists because it relates to temporary folder. So what I have to do is 1. setting the path where the file temporarily saved and 2. I have to tell Phpword that it should use that path to store generated file.
$path = './uploads/tmp'; // Set the temporary path
\PhpOffice\PhpWord\Settings::setTempDir($path); // Tell Phpword where the temporary path is
So this is the complete code
<?php
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Writer\Word2007;
class Test extends MY_Controller{
public function create()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$path = './uploads/tmp'; // Set the temporary path
\PhpOffice\PhpWord\Settings::setTempDir($path); // Tell Phpword where the temporary path is
$file = 'HelloWorld.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
}
}
Hope it can help those who get the same problem

Download file in Zend 1 (PHP): File name is japanese

I using Zend FW 1 to download file.
File need download have file name japanese : るファイルを選択.pdf
$this->path: is file path . Ex : D:\るファイルを選択.pdf
This is my code in PHP
public function send() {
if($this->checkPath()) {
// fileinfo extention enable
$type = mime_content_type($this->path);
if ($this->getRequest()->isSecure()) { // HTTPS sites - watch out for IE! KB812935 and KB316431.
header('Content-Description: File Transfer');
header('Cache-Control: max-age=10');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: ');
} else { //normal http - prevent caching at all cost
header('Content-Description: File Transfer');
header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=0');
header('Expires: '. gmdate('D, d M Y H:i:s', 0) .' GMT');
header('Pragma: no-cache');
}
if ($this->isIE()) {
$name = rawurlencode($this->name);
} else {
$name = $this->s($this->name);
}
$name = urlencode($name);
$Disposition = "attachment;filename*=UTF-8''$name";
$this->getResponse()->setHeader('Content-Type', $type . ";charset=utf-8")
->setHeader('Content-Disposition', $Disposition, true)
->setHeader('Content-Transfer-Encoding', 'binary', true)
->setHeader('X-Sendfile', readfile($this->path), true)
->sendResponse();
unlink($this->path);
}
}
But mime_content_type($this->path) retun :
mime_content_type(D:\るファイルを選択.pdf): failed to open stream: No such
file or directory in ...
I had set UTF-8 in getResponse
Try to use finfo
$result = new finfo();
echo $result->file($filename, FILEINFO_MIME_TYPE);

PDF::API2 How to return raw PDF

I have created a PDF using PDF::API2. How can I return the raw PDF?
Example:
my $pdf = PDF::API2->new();
$pdf = PDF::API2->open('./useful_scripts/invoice.pdf');
my $page = $pdf->openpage(1);
my $content = $page->text();
my $font = $pdf->corefont('Helvetica-Bold');
$content->fillcolor('black');
$content->font($font, 11);
$content->translate(170, 785);
$content->text($invoice->ott_invoice_number);
#Finally return raw $pdf
I am aware of the $pdf->saveas() method, but I don't actually want to save my PDF to a file.
IRC #perl-help answer:
my $raw_pdf = $pdf->stringify();
does the job.

How download application in zendframe work

I am using zend frame works . I want to include file download section in my application. I am using this code
> header('Content-Type: application/doc'); header('Pragma: no-cache');
> header('Content-Disposition: attachment; filename="'.$resume.'"');
> readfile(RESUME_PATH_WS . $resume);
But this code is not working .It return file with 0byte . Please help me how to i download files in zend frame work
public function downloadAction() {
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$filename = $this->_request->getParam('filename');
$filePath = folder/path to file/ . $filename;
if (file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: " . $fileSize);
header("Content-Disposition: attachment; filename=" . $fileName);
readfile($filePath);
exit();
} else {
die('The provided file path is not valid.');
}
}
Simply in your html side
Download

Downloaded FLV file is not working

Here is my code..
<?php
ob_clean();
$params = Zend_Controller_Front::getInstance()->getRequest()->getParams();
// block any attempt to the filesystem
if (isset($params['file']) && basename($params['file']) == $params['file']) {
$filename = $params['file'];
} else {
$filename = NULL;
}
// define error message
$err = '<div class="right-panel fl">
<h1> Download Question </h1> <a href="JavaScript:void(0)" class="button" onclick="javascript:history.go(-1)" > Back </a>
<div class="gradient-box" style="margin-top:20px;" ><p style="color:#990000">Sorry, the file you are requesting is unavailable.</p></div></div>';
if (!$filename) {
// if variable $filename is NULL or false display the message
echo $err;
} else {
// define the path to your download folder plus assign the file name
$path = BASE_PATH.QUESTIONS_FILE_PUBLIC.$filename;
$path2 = REL_PATH.QUESTIONS_FILE.$filename;
// check that file exists and is readable
if (file_exists($path) && is_readable($path)) {
// get the file size and send the http headers
$size = filesize($path);
header("Content-type: video/flv");
header('Content-Length: '.$size);
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
// open the file in binary read-only mode
// display the error messages if the file can´t be opened
echo file_get_contents($path);
//#readfile($path);
exit;
$file = # fopen($path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
} else {
echo $err;
}
}
?>
Try to use function from this comment to force browser to open download file dialog. It must help.