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.
Related
Can anyone help me to create simple .rtf file using php? For example: I have a page:
<table>
<tr><td>ID</td><tr><td>sth</td></tr>
<tr><td>1</td><tr><td>sth1</td></tr>
<tr><td>1</td><tr><td>sth1</td></tr>
<tr><td>1</td><tr><td>sth1</td></tr>
</table>
I want to create a .rtf file with this table to let user to edit it using program like MS Word. I was trying with this: http://books.msspace.net/mirrorbooks/php5/067232511X/ch28lev1sec1.html but it did not help me.
// $rtf holds your complete file data
// save file on your server 1st
file_put_contents('/path/to/outfile.rtf', $rtf);
// then offer it as download
header("Content-type: application/rtf");
echo file_get_contents("/path/to/outfile.rtf");
exit();
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 need to find a certain key in a pdf file. As far as I know the only way to do that is to interpret a pdf as txt file. I want to do this in PHP without installing a addon/framework/etc.
Thanks
You can certainly open a PDF file as text. PDF file format is actually a collection of objects. There is a header in the first line that tells you the version. You would then go to the bottom to find the offset to the start of the xref table that tells where all the objects are located. The contents of individual objects in the file, like graphics, are often binary and compressed. The 1.7 specification can be found here.
I found this function, hope it helps.
http://community.livejournal.com/php/295413.html
You can't just open the file as it is a binary dump of objects used to create the PDF display, including encoding, fonts, text, images. I wrote an blog post explaining how text is stored at http://pdf.jpedal.org/java-pdf-blog/bid/27187/Understanding-the-PDF-file-format-text-streams
Thank you all for your help. I owe you this piece of code:
// Proceed if file exists
if(file_exists($sourcePath)){
$pdfFile = fopen($sourcePath,"rb");
$data = fread($pdfFile, filesize($sourcePath));
fclose($pdfFile);
// Check if file is encrypted or not
if(stripos($data,$searchFor)){ // $searchFor = "/Encrypt"
$counterEncrypted++;
}else{
$counterNotEncrpyted++;
}
}else{
$counterNotExisting++;
}
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.
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');