I've developed an application that users can use to store lists of their favourite books, books they've read, books they'd like to read, etc., and it's all working fine, but it's just suddenly struck me that users may want to print off their list or download their list to use in another format such as a CSV file to import into a spreadsheet.
Obviously users could print the entire web page, but I'd like to give them the ability to just print their list, or to download it as say a CSV file for use in a spreadsheet.
What would be the easiest way to do this, apart from passing the query result to a HTML page and printing the HTML page?
For printing, most users appreciate the simplicity of a "cleaned-up" html page showing only/mostly the data they are interested in. In this fashion they can stay within the web browser to get a quick print of what they need. As indicated by Tom, this can often be achieved with the CSS whereby the undesired elements in a printout can be hidden and other elements may be shown with a slightly different style (for example to introduce more constrast, assuming a B/W printer etc.).
For exporting the data, you'll need to introduce a link / button which the users can click to signify their desire to get the data exported [to a particular format]. Your application can then serve this under the proper MIME type (i.e. you'll modify the http header 's Content-type (to be something like "Application/text") before streaming the CSV text)
To output a csv you can simply format the output from your query as a csv file, then set the content-disposition header for teh output. This snippet is taken from this website.
<?
$csv_output = "column 1,column2";
$csv_output .= "\n";
$result = mysql_query("select * from table");
while($row = mysql_fetch_array($result)) {
$csv_output .= "$row[col1],$row[col2]\n";
}
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".xls");
print $csv_output;
exit;
?>
Obviously users could print the entire web page, but I'd like to give them the ability to just print their list, or to download it as say a CSV file for use in a spreadsheet.
Why not provide a print stylesheet, simple things like hiding the nav and menu and increase the font size.
Related
I've got a variable that gets the pdf as a binary file into the website
<?=$calculationData->PDF_file?>
How do I show the pdf as a link, instead of binary text?
Note: I'm a newbie at this and don't know a lot about php! :-)
I assume $calculationData is an object from a database and the PDF content is stored there as binary data. Then you can do something like this:
Link to a PHP file that loads the content and sends it to the browser.
<a href="show_pdf.php?id=555">
Where id is the id of your calculationData somewhere in the database in this example. You need something to identify it.
show_pdf.php:
<?php
/* load your calculation data here ... */
header("Content-type: application/pdf");
header("Content-Disposition: attachment;filename='mywhateverfile.pdf'");
echo $calculationData->PDF_file;
exit();
In that way you can send the binary data to the browser and telling it with the correct headers, what to do with that data.
i have export a excel sheet which will collect data from my customer table (Mysql database) now i want to format it with column color.
here is my code to export excel sheet....
<?php
ob_start();
session_start();
include("include/session.php");
include("common_function.php");
//connect the database
$customer_id=$_GET['pid'];
//Enter the headings of the excel columns
$contents="Sr.,Agent Name,Contact Person,Contact Number,Password,Deal With Customer,References,Time to reach,Package Offered,Mode of Payment,Note,NEAREST CROSS STREET,DATE OF BIRTH\n";
//Mysql query to get records from datanbase
//You can customize the query to filter from particular date and month etc...Which will depends your database structure.
$sql = "SELECT id,agent_id,fname1,mobile_no,password,fname,rentfrom,cheque_no,package_id,monthly_monitoring,final_comment,cross_street,dob1 FROM customer WHERE `id`='$customer_id'";
$user_query = mysql_query($sql);
//While loop to fetch the records
while($row = mysql_fetch_array($user_query))
{
$contents.=$row['id'].",";
$contents.=$row['agent_id'].",";
$contents.=$row['fname1'].",";
$contents.=$row['mobile_no'].",";
$contents.=$row['password'].",";
$contents.=$row['fname'].",";
$contents.=$row['rentfrom'].",";
$contents.=$row['cheque_no'].",";
$contents.=$row['package_id'].",";
$contents.=$row['monthly_monitoring'].",";
$contents.=$row['final_comment'].",";
$contents.=$row['cross_street'].",";
$contents.=$row['dob1']."\n";
}
// remove html and php tags etc.
$contents = strip_tags($contents);
//header to make force download the file
header("Content-Disposition: attachment; filename=Sale_report".date('d-m-Y').".csv");
print $contents;
?>
now i want to color my first row...
Heay Cnik,
Ayyappan Sekar is right.
PHPExcel is a library that can be used to generate xls( and many other formats ) file. Its purpose is to generate a file and not sending it on some id.
What you can do is, first generate a file using PHPExcel library and save it somewhere.
For sending email to some id, you can use http://php.net/manual/en/function.mail.php'>mail function. However, its little complex to send emails with attachments using php's mail function though not very difficult.
Refer the links given below:
1] http://www.shotdev.com/php/php-mail/php-send-email-upload-form-attachment-file/
2] Attach File Through PHP Mail (refer to the answer of asprin)
3] http://www.texelate.co.uk/blog/send-email-attachment-with-php/
The CSV file is a simple text file, you can't format it with color
http://en.wikipedia.org/wiki/Comma-separated_values
Hi instead of using header to export to excel, U can use PHPExcel library. It provides much easier inbuilt methods to achieve what u want.. even u can apply styles, merge cells etc
I don't know whether this is happening thing or not. Anyways, my problem is
I have log function in my website, from where the Admin can view or visit the locations where the end users have visited. --> This is simple PHP
Now, if he wants to export the log he can but the exported CSV dont have links to the loations.
Hope you got.
I am posting code for your ref:
$contents="Sr.No,User_Type,Location\n";
$add = "select * from log_table order by id";
$user_query = mysql_query($add);
while($row = mysql_fetch_array($user_query))
{
$contents.=$row[log_id].",";
$contents.=$row[Type].",";
$contents.="<a href='$row[location]'>".$row[location]."</a>\n";
}
Header("Content-Disposition: attachment; filename=export.csv");
print $contents;
I had tried putting tag around Location. But it is not working.
What I Expect is
And What I got is
Advance Thanks =)
Excel isnt smart enough to handle CSV links. But is smart enough to handle regular HTML tables.
just output something like the following to a file called output.xls:
<table border=1>
<tr>
<td>1</td>
<td>employee</td>
<td><a href='http://www.example.com'>example</a></td>
</tr>
</table>
Excel will give you a notice that the file is not in the expected format, but if you confirm that you want to open it, you get what you want. Added bonus is that you can also use colors, width/height etc. For an admin that might be enough.
Another bonus is that you dont have to worry about different excel locales where some CSV require ; and others require ,
CSV doesn't support this type of formatting. That is, CSV is plain text. Microsoft Excel isn't automatically converting the links to be clickable.
If you want this, you will have to use an XLS Builder or some equivalent for whichever software you're looking to open it in.
Also, don't try to write your own CSV. Use fputcsv() if your'e going to continue working with a CSV format.
I have a intranet site running PHP 5 that needs to list a folder containing only Excel files.
Ideally the user needs to be able to input some search criteria (ie date) and the files would be filtered into a list of hyperlinks. These hyperlinks could then be selected to download the excel file.
What I have so far is:
//get search parameters (from a form)
$s_Date = $_GET['s_Date'];
$s_Shift = $_GET['s_Shift'];
$s_Name = $_GET['s_Name'];
//search folder
foreach(glob("c:\folderA\folderB\*".$s_Date."*".$s_Shift."*".$s_Name."*.*") as $FileName)
{
echo basename($FileName);
echo"<a href=?myad=".basename($FileName)."/>Download</a>"."<br />";
}
This returns list of files but selecting hyperlinks doesn't prompt for download.
How do I get the hyperlink to force a content-type of msexcel?
Assuming you are using a PHP script to deliver the file conent, the script needs to set the Content-Type header:
<?
header('Content-Type: application/excel');
header('Content-Disposition: attachment;filename=myfile.xls');
// send file content
?>
Note you can also set the name to use for the file using the Content-Disposition header.
You might also want to consider a more well-formed <a> tag that points to your download script:
echo 'Download<br />';
In this example, you need to replace dl.php with your actual script.
Also append Content-Length header:
header('Content-Length : ' . filesize('myfile.xls'));
Client would like to know how big file is and how much of it has already been downloaded.
I have an RTF file that I want to display inside a web page after tags have been replaced with user input.
I would like to be able to display the RTF file without having to convert it to something before displaying it.
Every time I try it now it gives me the popup open/save box even though I am telling it to display it inline with:
header("Content-type: application/msword");
header("Content-Disposition: inline; filename=mark.rtf");
header("Content-length: " . strlen($output));
echo $output;
Most browsers won't reliably display RTF content. It IS possible to parse the RTF into HTML, and display the HTML content on your web page however.
You need some kind of program to parse RTF and convert it to HTML. I'm assuming it has to be free. I do not know of any reliable free RTF parsing or RTF to HTML libraries in PHP.
I recommend you use a command-line conversion program like RTF2HTML: http://sageshome.net/?w=downloads/soft/RTF2HTML.html
You would need to download and install this program on your webserver, allow the user to upload the file to a temp directory, and then call the command line application from PHP with shell_exec():
$html_output_path = '/path/for/processing/files/'
$html_output_filename = $username . $timestamp;
if (is_uploaded_file($_FILES['userfile']['tmp_name'])
{
shell_exec('rtf2html ' .
escapeshellarg($_FILES['userfile']['tmp_name']) . " " .
$html_output_path . $html_output_filename);
}
$html_to_display = file_get_contents($html_output_path .
$html_output_filename);
Then, parse the results as HTML and display them. Not a bad strategy. Note that you will probably need to remove the head, body and possibly other tags if you're going to display the content inside another web page.
You might want to check out https://github.com/tbluemel/rtf.js for client-side RTF rendering. It's still in its early stages but it renders even embedded graphics. Support for rendering embedded WMF artwork is still very very limited, though, and requires browser support for the tag.
You needed an RTF to HTML converter written in PHP. I think this page contains your solution:
http://www.websofia.com/2014/05/a-working-rtf-to-html-converter-in-php/
First: you've got your content-type wrong. for RTF it's text/rtf
Second: you'll only be able to display in-line this type of content, which can be rendered by the web browser. RTF is not one of these. So you won't be able to display it in-line without converting it, or without some plug-in for the browser. Of course conversion might be on-the-fly.
Web pages can only contain HTML. You would need a browser plugin like flash to display other file types. See Scribd for example.
This isn't exactly an answer to your question, but one thing you may want to do is remove the "filename=mark.rtf" from the header. I've had browsers treat something as a download if I include "filename" in the header, even if the "Content-Disposition" is "inline".
You can't just output a file from within PHP code. You need to extract the data from it, then print the contents inline.
The php function 'file_get_contents' may do what you need. The functions manual is here: http://us2.php.net/filegetcontents
A sample usage is here:
$contents = file_get_contents('yourfile.rtf');