0

Using PHP, I can convert MySQL data or static table data to csv, Excel, JSON, MySQL etc but is there a useful conversion script or tool that can convert table data into other formatted/styled formats such as PDF and/or JPG/PNG using the PHP GD Library or other?

3 Answers 3

1

I've used this before to turn a HTML table into a PDF. I generated the table from a MySQL query.

Sign up to request clarification or add additional context in comments.

1 Comment

Looks like this has moved to code.google.com/p/dompdf and I'd be interested to see some examples similar to this but for images. There's a lot of "screenshot" software apps I guess works in the interim...
0

To export to Excel I use the following code:

<?php

/*  Define our Database and Table Info */


$username="";
$password="";
$database="";
$table="";


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$select = "SELECT * FROM $table";                
$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($i = 0; $i < $fields; $i++) {
    $header .= mysql_field_name($export, $i) . "\t";
}

while($row = mysql_fetch_row($export)) {
    $line = '';
    foreach($row as $value) {                                            
        if ((!isset($value)) OR ($value == "")) {
            $value = "\t";
        } else {
            $value = str_replace('"', '""', $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);

if ($data == "") {
    $data = "\n(0) Records Found!\n";                        
}

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=mailinglist.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data"; 
?> 

Now be careful with how you include this. It's using the Headers to send the file information to force a download, by doing this you can't have any white space anywhere before these headers are sent otherwise it may throw an error. I usually have this link open as a new window to prevent anything from happening... Again this is just a pretty basic script that can be expanded greatly. Hope this Helps!

2 Comments

It could be useful to combine with a csv, xls converter of which I do have implementations, I'd like to add pdf and jpg to the mix also with a simple drop down or get variable posted to determine the output file format. Thanks for the info anyway.
As far as PDF I normally end up using a library like: fpdf.org (FPDF) it's pretty easy to impliment and pretty simple to use. I know there is scripting to make JPEGs on the fly as well as flash but I haven't really experimented with them too much.
0
<?php  

/  Define our Database and Table Info /  

$username="";  
$password="";  
$database="";  
$table="";  

mysql_connect(localhost,$username,$password);  
@mysql_select_db($database) or die( "Unable to select database");  
$select = "SELECT * FROM $table";                  
$export = mysql_query($select);  
$fields = mysql_num_fields($export);  

for ($i = 0; $i < $fields; $i++) {  
    $header .= mysql_field_name($export, $i) . ",";  
}  

while($row = mysql_fetch_row($export)) {  
    $line = '';  
    foreach($row as $value) {                                              
        if ((!isset($value)) OR ($value == "")) {  
            $value = ",";  
        } else {  
            $value = str_replace('"', '""', $value);  
            $value = '"' . $value . '"' . ",";  
        }  
        $line .= $value;  
    }  
    $data .= trim($line)."n";  
}  
$data = str_replace("r","",$data);  

if ($data == "") {  
    $data = "n(0) Records Found!n";                          
}  

header("Content-type: application/x-msdownload");  
header("Content-Disposition: attachment; filename=mailinglist.xls");  
header("Pragma: no-cache");  
header("Expires: 0");  
print "$headern$data";   
?> 

1 Comment

You could add a description of what your code does to make your answer clearer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.