Jump to content

CSV > PHP, and then PDF - is it possible... ?


burnmic

Recommended Posts

Hi,

 

I'm relatively new to PHP, and have set myself a challenge to get something done.

I guess this isn't so much a question, but more me asking for advice really.. i.e., can it be done?!

 

I'm looking to do the following:

 

Stage 1:

 

1. Create a form that allows me to upload a .csv file to a directory on my server [done]

2. Create a script that reads the csv file during the upload, takes out a value from a specific line from within the .csv file (line 3 in this example) and use this variable as the value to rename the .csv file.

 

The form is complete, and allows me to upload a file into a directory.

I can read the name of the file using the following code:

 

$filename = basename( $_FILES['file']['name']);

 

..but can't for the life of me workout how to read a csv file, locate a specific string, and set this string as a variable to rename the file.  Any ideas?

 

Stage 2:

Once the file is saved with it's new unique name, I need to read the data & style it (I already have a stylesheet set).  I can read and render the data from the file using this code:

 

<?php
$row = 1;
$handle = fopen("report/mycsvfile.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    echo "<p> $num fields in line $row: <br /></p>\n";
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";
    }
}
fclose($handle);
?>

 

Is there a way to add styling to this using div id's / classes without needing to enter the data into the csv file first, or copy the rendered html into another file? (maybe I could save the html output and read that into another file.. not sure).

 

Stage 3:

 

Finally, once I've managed to read the data, rename the file & style the page, I would like to be able to save the page automatically into a PDF format.  I've looked at a lot of pdf apps and a php pdf library also, but really need to be able to save a rendered page (with website branding and styled csv data) into the pdf.  Does anybody know whether this is possible?

 

Not bad for my first post... a reasonably tall order i'm sure!

 

I guess once I know it's possible, it's just a case of research research research and a lot of hard work :)

 

hope to hear from you soon,

mic

Link to comment
https://forums.phpfreaks.com/topic/144814-csv-php-and-then-pdf-is-it-possible/
Share on other sites

1. Possible :) See fgetcsv.

2. Probably possible... I'm not much into HTML and CSS though.

3. That's probably the hardest part. I don't know of any PDF library, that will let you convert any HTML, as it is displayed in browser, into PDF. You might however consider creating a standard layout for your PDF files, and then just dynamiccaly add content from CSV.

Hey Mchl - thanks for the advice!  ;D

 

I'm looking fgetcsv now, and will hopefully work this part out shorty (in beginners terms, around 6 hours!).

 

With regards to your idea on Stage 3, having a default pre-designed layout for the PDF and adding the content from the csv dynamically - this sounds almost too good to be true, can that actually be done?

 

Thanks again for your feedback, it's really appreciated :)

What I meant was: you can create a PHP code, that will create a common elements of your layout in PDF (like website's logo, header, footer etc). Then use separate code to fill it with data.

 

As far as I know it is not possible to use a ready PDF template to fill it with content without the use of some external binaries.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.