Downloading excel file from controller in code igniter - php

I am having the following issue:
I am using code igniter as php framework, and from one of my views I do an ajax call to a function that generates and xlsx file using PHPExcel and data from mysql database. The file is correctly created in the server but when I try to force_download it won't download anything. Here is the php function:
public function generar_excel($idCotizacion){
isLogged($this->session->userdata('logged_in'));
isAdmin($this->session->userdata('logged_in'));
$cotizacion = $this->cotizacion_model->get_cotizacion_by_id($idCotizacion);
$propuestas = $this->propuesta_model->getPropuestasPorCotizacion($idCotizacion);
error_reporting(E_ALL);
date_default_timezone_set('Europe/London');
require_once 'application/libraries/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php';
require_once 'application/libraries/PHPExcel-1.8/Classes/PHPExcel.php';
$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('prueba.xlsx'); // Empty Sheet
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('C8', $cotizacion["capitas"])
->setCellValue('C2', $cotizacion["empresa_nombre"])
->setCellValue('C9', $cotizacion["masa_salarial"])
->setCellValue('B11', $cotizacion["aseguradora_actual"])
->setCellValue('B13', $cotizacion["variable"]/100)
->setCellValue('B14', '0.6')
->setCellValue('B12', '0');
$letra = 'C';
foreach($propuestas->result_array() as $row) {
$excel2->getActiveSheet()->setCellValue($letra.'11', $row["nombre"])
->setCellValue($letra.'13', $row["variable"]/100)
->setCellValue($letra.'14', '0.6')
->setCellValue($letra.'12', '0')
->setCellValue($letra.'16', '=C$8*'.$letra.'12+C$9*'.$letra.'13+C$8*'.$letra.'14')
->setCellValue($letra.'17','=(B$16-'.$letra.'16)*13')
->setCellValue($letra.'18','=1-('.$letra.'16/B16)');
++$letra;
}
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$nombreArchivo = 'CuadroComparativo-'.$cotizacion["empresa_nombre"].'.xlsx';
$objWriter->save('uploads/'.$nombreArchivo);
$fileContents = file_get_contents('http://rpm.singleton.com.ar/uploads/'.$nombreArchivo);
//print_r($fileContents);
$this->load->helper('download');
force_download($nombreArchivo, $fileContents);
}
The response and preview from inspecting the browser are unreadable.
Thank you!

Set the MIME type for the data you're sending in the force_download() function. The browser may be either trying to best guess it, or just outputting exactly what you send it (which may be the 'unreadable' data you're referring to).
Try changing your force_download line to:
force_download($nombreArchivo, $fileContents, TRUE);
This will set the MIME type based on your file extension (xlsx), with should force the browser to download the file.

I solved it by adding in the success function of ajax the following:
success: function(json) {
var content = JSON.parse(json);
//alert(content);
if (content.error) {
$('#error').html(content.response);
$('#error').show();
$('#success').hide();
} else {
// alert(content);
location.href="http://rpm.singleton.com.ar/uploads/"+content.response;
//location.href = "<?php echo site_url('administrador'); ?>/" + content.response;
}
},
And in the php i echo the file name.

Try this code:
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename={$nombreArchivo}.xls");
$this -> load -> view ('view_file',$fileContents);

Related

how to convert page to pdf in php

I have an html page like JsFiddle and I want convert this in pdf, i can't create the line to line pdf because the page is dinamically create, I use php for calling a fiel that connect to mysql and fill a template file like.
$_POST['IdQuestionario']=5;
$_POST['IdUtente']=10001;
$_POST['Visualizza']=true;
$_POST['IdImpianto']=1;
$_POST['Stampa']=true;
$_POST['TipoImpianto']='grande';
ob_start();
ob_clean();
require_once 'intro.php';
$tbl=ob_get_clean();
$html.=$tbl;
I'm trying with tcpf, mpdf , jsPDF but i cant obtain a discrete output because I use colgroup for table. anyone say me a method for render the page,if is possible whitout install software on server.
There a few that i know of - some have problems with tables, I would avoid DOMPDF - known issues with tables.
There's one that's recommended from cvision; i don't have a code sample, but you can download it free and even sample it online.
There's also a php-pdf product available from muhimbi (a little lesser-known!, but i think it's free)
<?php
// Include the generated proxy classes
require_once "documentConverterServices.php";
// Check the uploaded file
if ($_FILES["file"]["error"] > 0)
{
echo "Error uploading file: " . $_FILES["file"]["error"];
}
else
{
// Get the uploaded file content
$sourceFile = file_get_contents($_FILES["file"]["tmp_name"]);
// Create OpenOptions
$openOptions = new OpenOptions();
// set file name and extension
$openOptions->FileExtension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
$openOptions->OriginalFileName = $_FILES["file"]["name"];
// Create conversionSettings
$conversionSettings = new ConversionSettings();
// Set the output format
if(isset($_POST["outputFormat"]))
{
$conversionSettings->Format = $_POST["outputFormat"];
} else {
$conversionSettings->Format = "PDF";
}
// Set fidelity
$conversionSettings->Fidelity = "Full";
// These values must be set to empty strings or actual passwords when converting to non PDF formats
$conversionSettings->OpenPassword="";
$conversionSettings->OwnerPassword="";
// Set some of the other conversion settings. Completely optional and just an example
$conversionSettings->StartPage = 0;
$conversionSettings->EndPage = 0;
$conversionSettings->Range = "VisibleDocuments";
$conversionSettings->Quality = "OptimizeForPrint";
$conversionSettings->PDFProfile = "PDF_1_5";
$conversionSettings->GenerateBookmarks = "Automatic";
$conversionSettings->PageOrientation="Default";
// Create the Convert parameter that is send to the server
$convert = new Convert($sourceFile, $openOptions, $conversionSettings);
// Create the service client and point it to the correct Conversion Service
$url = "http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl";
$serviceClient = new DocumentConverterService(array(), $url);
// If you are expecting long running operations then consider longer timeouts
ini_set('default_socket_timeout', 60);
try
{
// Execute the web service call
$result = $serviceClient->Convert($convert)->ConvertResult;
// Send the resulting file to the client.
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"convert." . $conversionSettings->Format . "\"");
echo $result;
}
catch (Exception $e)
{
print "Error converting document: ".$e->getMessage();
}
}
?>
Also, you could investigate 'Snappy' (has dependencies)
You can try WKHTMLTOPDF.
Here is a Stackoverflow Thread on how to use it with PHP.
How do I get WKHTMLTOPDF to execute via PHP?
And here is a wrapper for PHP
https://github.com/mikehaertl/phpwkhtmltopdf
MPDF one of best library to convert pdf, try it
MPDF link : http://www.mpdf1.com/mpdf/index.php
Example link : http://mpdf1.com/common/mpdf/examples/

Mozilla pdf.js, How to I specify the filename for download?

I pass the location of the php file that contains the following code as parameter to the viewer.html file and it is displayed correctly but when clicking the download button in the pdf viewer the document name is always document.pdf. This poses a problem because of how many mobile users will be downloading files only to discover that all of their files have the the name document.pdf and that they (for most mobile browsers) can't change the filename before downloading.
Do I have to pass some arbitrary parameter to the file or redirect to self with the filename appended?
<?php
$content = "a binary representation of my pdf";
header("Content-type: application/pdf");
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="someFile.pdf"');
echo $content;
?>
I've run into this same issue. From the pdf.js's viewer.js source:
function getPDFFileNameFromURL(url) {
var reURI = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
// SCHEME HOST 1.PATH 2.QUERY 3.REF
// Pattern to get last matching NAME.pdf
var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
var splitURI = reURI.exec(url);
var suggestedFilename = reFilename.exec(splitURI[1]) ||
reFilename.exec(splitURI[2]) ||
reFilename.exec(splitURI[3]);
if (suggestedFilename) {
suggestedFilename = suggestedFilename[0];
if (suggestedFilename.indexOf('%') != -1) {
// URL-encoded %2Fpath%2Fto%2Ffile.pdf should be file.pdf
try {
suggestedFilename =
reFilename.exec(decodeURIComponent(suggestedFilename))[0];
} catch(e) { // Possible (extremely rare) errors:
// URIError "Malformed URI", e.g. for "%AA.pdf"
// TypeError "null has no properties", e.g. for "%2F.pdf"
}
}
}
return suggestedFilename || 'document.pdf';
}
So the majic needs to come from the URL via the reURI regexp.
What you need to do is this:
http://domain.com/path/to/Named.pdf
http://domain.com/path/to/your/api?fileId=123&saveName=Named.pdf
Each of these will result in a save as filename of Named.pdf thanks to the regexp code above.
Based on comments
You can add this to wherever you're using the viewer.js file.
setTimeout(() => {
// Wait for PDFViewerApplication object to exist
PDFViewerApplication.setTitleUsingUrl('custom-file.pdf');
}, 10);
Then when you download the PDF it will have that filename

sending an uploaded PDF file via POST is becoming corrupt

Im trying to send a pdf file uploaded using JQ to a PHP file which uses CLI and pdf2svg to convert it to an svg file, with the eventual aim to return that svg file so that it can be placed into svg editor.
Currently i have this:
In svg-editor.js:
if(file.type.indexOf("pdf") != -1){
//convert to svg
//load svg string
var reader = new FileReader();
reader.onloadend = function(e) {
$.post("pdfuploads/pdfconvert.php", { 'pdf[]': [e.target.result] })
.done(function(data){
alert("Data Loaded: " + data );
svgCanvas.importSvgString(data, true);
svgCanvas.ungroupSelectedElement()
svgCanvas.ungroupSelectedElement()
svgCanvas.groupSelectedElements()
svgCanvas.alignSelectedElements("m", "page")
svgCanvas.alignSelectedElements("c", "page")
});
};
reader.readAsText(file);
}
The above checks the extension of the file being loaded, if its pdf then it posts it to the php file. (all working fine)
In my php file (its being tied in with drupal):
<?php
$dir = getcwd();
define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']); //added to make sure its defined as we're outside the use of index.php
chdir(DRUPAL_ROOT);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
chdir($dir);
global $base_url;
$time = microtime();
$handle = fopen("pdfupload-".$time.".pdf", "wb");
if (fwrite($handle, file_get_contents($_POST['pdf'][0])) === FALSE) {
$error = "Cannot write to file";
exit;
}
fclose($handle);
//$file = file_put_contents('pdfupload-'.$time.'.pdf', $_POST['pdf'][0]);
$svg = exec("/usr/local/bin/pdf2svg pdfupload-".$time.".pdf output_page-".$time."%d.svg all");
cg_utils_watch('file pdf value','', $error);
cg_utils_watch('file svg value','', $svg);
echo "<pre>";
print_r($svg);
echo "</pre>";
return $svg;
Ignore the logging and the fact that $svg doesnt return the svg file --
The pdf stream is taken from post, and saved. Unfortunalty someware between upload and saving on the server the pdf file becomes corrupted. It saves a pdf file, but when opening that file its empty, the end result being the svg file thats written to the server in the exec() is also empty.
Does anyone have any ideas / know any better way of doing this?
ok, so turns out that mkl in the comments above was right, the issue was with the reader.readAsText(file) line.
This was fireing before the post, so the PDF file was being posted as text.
following this: http://www.develop.com/htmlfivefileapi i changed the JQ to the following:
if(file.type.indexOf("pdf") != -1){
//convert to svg
//load svg string
var reader = new FileReader();
reader.onloadend = function(e) {
$.post("pdfuploads/pdfconvert.php", { 'pdf[]': [e.target.result] })
.done(function(data){
alert("Data Loaded: " + data );
svgCanvas.importSvgString(data, true);
svgCanvas.ungroupSelectedElement()
svgCanvas.ungroupSelectedElement()
svgCanvas.groupSelectedElements()
svgCanvas.alignSelectedElements("m", "page")
svgCanvas.alignSelectedElements("c", "page")
});
};
reader.readAsDataURL(file);
}
and it now works like a charm :)

File only downloading when opened in new window/tab

I have a function that creates different file types depending on a variable, I have it generating an XML, but when I click the link to the page to do so (XML), nothing happens. If I click to open it in a new tab or manually enter the url in the title bar then the file will download as I want it to.
function asset($asset_id, $display = ''){
$this->load->model('model_asset');
$asset = $this->model_asset->get_by_id($asset_id, true);
switch($display) {
case 'xml':
$this->load->helper('array_to_xml_helper');
$asset_arr = get_object_vars($asset);
$filename = $asset->title .'-'. $asset->subtitle . '.xml';
$xml = Array2XML::createXML('asset', $asset_arr);
header ("Content-Type:text/xml");
header('Content-Disposition: attachment; filename="'. $filename .'"');
echo $xml->saveXML();
break;
}
}
How can I make this work with dynamically generated files (I'm using a arraytoxml utility function I found here)
You can try to set file in attachement
Add this to your headers :
Content-disposition: attachment
filename=huge_document.pdf

PHP "save dialog" for file received from web service

I'm using WSO2 WS Framework and I managed to run example in which web service returns an image as a MTOM attachment which is then saved by the client using file_put_contents(...) command.
Service:
<?php
function sendAttachment($msg){
$responsePayloadString = <<<XML
<ns1:download xmlns:ns1="http://wso2.org/wsfphp/samples/mtom">
<ns1:fileName>test.jpg</ns1:fileName>
<ns1:image xmlmime:contentType="image/jpeg" xmlns:xmlmime="http://www.w3.org/2004/06/xmlmime">
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:myid1"></xop:Include>
</ns1:image>
</ns1:download>
XML;
$f = file_get_contents("test.jpg");
$responseMessage = new WSMessage($responsePayloadString,
array( "attachments" => array("myid1" => $f)));
return $responseMessage;
}
$operations = array("download" => "sendAttachment");
$service = new WSService(array("operations" => $operations, "useMTOM" => TRUE));
$service->reply();
?>
Client:
<?php
$requestPayloadString = '<download></download>';
try {
$client = new WSClient(
array( "to" => "http://SPLINTER/MTOM/service.php",
"useMTOM" => TRUE,
"responseXOP" => TRUE));
$requestMessage = new WSMessage($requestPayloadString);
$responseMessage = $client->request($requestMessage);
printf("Response = %s \n", $responseMessage->str);
$cid2stringMap = $responseMessage->attachments;
$cid2contentMap = $responseMessage->cid2contentType;
$imageName;
if($cid2stringMap && $cid2contentMap){
foreach($cid2stringMap as $i=>$value){
$f = $cid2stringMap[$i];
$contentType = $cid2contentMap[$i];
if(strcmp($contentType,"image/jpeg") ==0){
$imageName = $i."."."jpg";
if(stristr(PHP_OS, 'WIN')) {
file_put_contents($imageName, $f);
}else{
file_put_contents("/tmp/".$imageName, $f);
}
}
}
}else{
printf("attachments not received ");
}
} catch (Exception $e) {
if ($e instanceof WSFault) {
printf("Soap Fault: %s\n", $e->Reason);
} else {
printf("Message = %s\n",$e->getMessage());
}
}
?>
Instead of that I would like to open a "Save dialog" to choose between opening or saving the file. When searching for solution I read about setting heders like:
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="test.jpg"');
But it didn't work well. "Save dialog" poped up, but when image couldn't be opened saying that file is empty.
Actually I don't understand very good how this MTOM attachments thing is working. In client code, I think $f is a string and when I do printf($f) it prints 0(zero) so how can I save this string as an image?
If you want to use that headers, you have to output the file content, not save it somewhere.
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename="test.jpg"');
// Output file. This must be the ONLY output of the whole script
echo $rawFileContents;
The basics is that at the moment you have your whole file content loaded in a variable (and it seems is $f in your code), you output it instead of write it in a file (as I think you're doing now). So, the three lines of code I gave you should replace the file_put_contents() calls.
Instead, if you want to save the file in your /tmp folder, ok, do it, but then use instead
header('Location: /tmp/' . $imageName);
This way you redirect the user browser directly to the saved file, and let users do what they want with it.

Categories