I am using dompdf to create a pdf file out of an html file that gets created on-the-fly for the sole purpose of it serving as input for the pdf generator, however I am having trouble doing this, I implemented the code in this thread and everything works fine (I could output a simple pdf) however when I try to give it a more specific url I get this error:
An Error Was Encountered Unable to
load the requested file
here's the code that has the problem:
function printPDF(){
//write_file() usa un helper (file)
$this->load->library('table');
$this->load->plugin('to_pdf');
// page info here, db calls, etc.
$query = $this->db->get('producto');
$data['table'] = $this->table->generate($query);
$path_url = base_url().'print/existencias.html';
write_file($path_url, $data['table']);
$html = $this->load->view($path_url, 'consulta', true);
pdf_create($html, 'consulta');
}
Not sure about the exact problem, but please check this:
1) as stated in CI's manual, load->view's second parameter should be an associative array or an objet, translated to vars via extract. That may generate some problem generating $html.
2) try making $path_url relative to application/views directory, as read in CI's manual.
you should use tcpdf to create a pdf.
//create controller for example :
public function create_pdf()
{
$this->load->library("Pdf");
$data['results'] = // your data
$this->load->view('pdfview',$data);
}
//pdfview is the page having tcpdf code and your pdf code.
You can try it like this
public function export_pdf() {
$filename = 'FILENAME.pdf';
header("Content-Description: Advertise Report");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/pdf; ");
$file = fopen('php://output', 'w');
$header = array("Question", "Answer", "Name", "Email", "Phone", "Date");
fputpdf($file, $header);
fputpdf($file, 'YOUR DATA');
fclose($file);
exit;
}
Related
Data in csv file is not in proper format it show like as javascript code or html code in only one column
Code in userscontroller
for export data in csvfile from database of user table
public function export() {
$this->response->download("export.csv");
$data = $this->Users->find('all');
$this->set(compact('data'));
$this->layout = 'ajax';
return;
}
// app/Views/Users/export
header('Content-type: text/csv');
header("Content-Disposition: attachment; filename=export.csv");
foreach ($data as $row) {
foreach ($row['Users'] as $cell) {
// Escape double quotation marks
$cell = '"' . preg_replace('/"/','""',$cell) . '"';
}
echo implode(',', $row['Users']) . "\n";
}
Screenshot of output:
As I see here, Look like it is debug variable for debug bar or something like that (cause i not have full view of your csv file)
So I can suggest some action to check as i know:
Check "your template" file as see $this->layout = 'ajax'; . It may contain default debug bar or custom code. Empty ajax layout look like this
Because CSV file is not normal view. So better you just set this action not use any view layout by using $this->layout = false; or in Cake 3 use $this->viewBuilder()->layout(false);
Sometime php code also inject to output file or view page because your code have notice or warning message. So please check the full csv file, start read from where html code begin and you will understand the problem
I am new to CodeIgniter and was working on downloading a file. However, I want to download a file that resides on my local machine, I am able to locate the file by providing the path, also the file gets downloaded with a File type, However, I want my file to be in .csv format
Here goes my Controller's download function:
public function download()
{
$state_id=$this->input->post('state'); // gets me the state-id from viw's dropdown
$this->load->helper('download'); // Load download helper
$file="C:\\Users\usernew\\Desktop\\New folder\\".$state_id.".csv";
$filename=$state_id.'.csv';
if (file_exists($file))
{
$data = file_get_contents($file); //check file exists
force_download($fileName,$data);
}
else{
echo"not working!";
}
}
Where am I going wrong?
The force_download() takes in two parameters, i.e. the file name and the data to be written to that file. As the file already consists of some sample data, therefore, the function goes like this:
public function download()
{
$state_id=$this->input->post('state'); // gets me the state-id from viw's dropdown
$this->load->helper('download'); // Load download helper
$filename=$state_id.'.csv';
$file="C:\\Users\usernew\\Desktop\\New folder\\".$filename;
if (file_exists($file)) //check file exists
{
force_download($file,NULL); //NULL as the file already has data
}
else{
echo"not working!";
}
}
you can use header function instead force_download:
$file="C:\\Users\usernew\\Desktop\\New folder\\".$state_id.".csv";
$filename=$state_id.'.csv';
header("Content-type: text/csv");
header('Content-Disposition: attachment; filename='.$filename);
readfile( $file );
Hi everyone! I'm working on a Laravel 5.2 application in which the user can download a variety of files. One of these is a 'User guide', explaining how the website is set up and functionalities etc. I would like the PDF to be streamed in another page, so that the user is still within the application. The controller which I am using is:
public function userguidePDF(){
return response()->stream('../public/download/userguide.pdf');
}
But this returns:
Argument 1 passed to
Symfony\Component\HttpFoundation\StreamedResponse::__construct() must
be callable, string given, called in
/path/to/laravel/vendor/laravel/framework/src/Illuminate/Routing/ResponseFactory.php
on line 117 and defined
I have searched on the internet for a method, which leaded me to the following syntax:
return response()->stream($callback, 200, $headers);
Unfortunately, I am unable to find more documentation on the parameters because I don't understand them. Could someone please explain to me what the $callback, 200, $header are as parameters and how I can use this?
https://laravel.com/docs/5.2/responses#view-responses
public function userguidePDF() {
return response()->file(
public_path('download/userguide.pdf')
);
}
$callback should be a function that ouputs your PDF. For example:
$callback = function()
{
$handle = fopen("../public/download/userguide.pdf", "r");
$filecontent= fread($handle, filesize("../public/download/userguide.pdf"));
fclose($handle);
return $filecontent;
};
You can use download also
return Response::download($file, 'filename.pdf', $headers);
here is the documentation for streaming and also downlaod.
https://laravel.com/api/5.2/Illuminate/Routing/ResponseFactory.html#method_stream
EDIT
Use headers like this
// We'll be outputting a PDF
header('Content-Type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
I am using HTML2PDF library in codeigniter.I am trying to generate bulk pdf using it.
In which i am facing issue like same content in every pdf or pdf have no content.I have already did my homework.Yeah but there is always showing perfect for generated first pdf (For account :3)
As per me there is must be issue of below code :
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
Issue : It works for first time but for second time it flush all the content of content variable and due to that duplicate PDF or without content PDF generate.
I have tried like below
1) create object in generatetemplate.php and pass to common.php
2) tried with include_once //getting same conent in every pdf and if i am doing echo then showing no content for 2nd and 3rd pdf
File structure :
application
controllers
generatetemplate.php
libraries
common.php
html2pdf
html2pdf.php
template.php
common.php :
function print_content($customerdata){
$this->load->library('/html2pdf/html2pdf');
$template_config=$this->config->item('template');
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
$content = str_replace("<CUSTOMER_ADDRESS>",$CUSTOMER_ADDRESS,$content);
$this->CI->html2pdf->pdf->SetDisplayMode('fullpage');
$this->CI->html2pdf->writeHTML($content);
$this->CI->html2pdf->Output($download_path,"F");
}
generatetemplate.php
function __construct() {
parent::__construct();
$this->load->library("common");
$this->load->library('html2pdf');
}
function get_customer_data(){
$this->db->order_by("id","DESC");
$this->db->where('id IN (1,2,3)');
$query = $this->db->get("customers")->result_array();
foreach($query as $key=>$accountdata){
$this->common->print_content($accountdata);
}
}
Any help and ideas will be appreciated.
I have tried below code and its work for me.
Common.php
function print_content($customerdata){
$this->load->library('/html2pdf/html2pdf');
$template_config=$this->config->item('template');
ob_start();
require_once($template_config.'template.php'); //
$content = ob_get_contents();
ob_clean();
$content = str_replace("<CUSTOMER_ADDRESS>",$CUSTOMER_ADDRESS,$content);
$this->CI->html2pdf = new HTML2PDF('P','A4','en'); // Just added this line and its work for me.
$this->CI->html2pdf->pdf->SetDisplayMode('fullpage');
$this->CI->html2pdf->writeHTML($content);
$this->CI->html2pdf->Output($download_path,"F");
}
I exported the reviews from my magento website in csv format. Reviews contains :,;,?,! etc. Normally reviews are well in fields. When the review contain ; ,it's get splitted to next field. (I opened the csv file using OpenOffice spreadsheet)
For Example:
Review -> It's nice to use; looks good.
Now output ->
Field A-> It's nice to use
Field B-> looks good
Expected Output
Field A ->It's nice to use; looks good
I used the following header to export as acsv file in php
header('Content-type: application/utf-8');
header('Content-disposition: attachment; filename="file.csv"');
How can I get my expected output?
I would advise tackling the problem from the other side - I believe this is a bone of contention for Magento as some users want the semi-colon.
I would suggest looking at the way you use openoffice - It has been a while but I believe there is an option during the load of the csv called "Separator options" - Change these and all should work well.
The sad thing is if this is the correct answer it is off topic :-( but I hope this helps you achieve your goal!
Thanks,
//P
I'm using this class I've made for exporting data as a CSV spreadsheet. The trick is fputcsv() which should do the formatting right. Then, of course, you must properly import it to OpenOffice (or LibreOffice now), but that's different story.
Maybe it'll help you out.
<?php
class Spreadsheet {
private $grid = array();
/**
* Set cell value at position X, Y
*/
public function setCell($x, $y, $value) {
for($yy=0; $yy<=$y; $yy++) {
if(!isset($this->grid[$yy])) {
$this->grid[$yy] = array();
}
}
for($xx=0; $xx<=$x; $xx++) {
if(!isset($this->grid[$y][$xx])) {
$this->grid[$y][$xx] = null;
}
}
$this->grid[$y][$x] = $value;
}
/**
* final command executed, including "exit".
* Sends CSV to browser.
*/
public function sendAsCsv(/*string*/ $filename) {
header('Content-type: text/csv; charset=utf-8');
header('Content-Language: cs');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header("Pragma: no-cache");
header("Expires: 0");
ob_end_clean();
$outputBuffer = fopen("php://output", 'w');
$this->appendToFile($outputBuffer);
fclose($outputBuffer);
exit;
}
/**
* Appends content to a file (rarely used outside this class)
*/
public function appendToFile(/*resource*/ $fileHandle) {
foreach($this->grid as $val) {
fputcsv($fileHandle, $val);
}
}
}