Norman Bird
Norman Bird

Reputation: 682

save MYSQL DB info to a PDF file for users

I have an online journal/diary site, where users save their diary entries to my DB via PHP and MYSQLi.

I want to add a feature where the user can save all their entries to a PDF file, with the click of a menu link.

What is the best way to do this? I have looked into DOMPDF, and it seemed easy and promising, but getting it to work has taken up days and im ready to look at other solutions.

Maybe many solutions out there and I don't want to spend 3 days just to find out, it is not for me, so I'm asking for those who have done something like this, what worked for you.

UPDATE I ws able to get dompdf working and it is fairly easy and works nice. The issue I encountered is that it crashes if it tries to render a large amount of HTML. Seems to be memory issues with code(Google consensus). For instance. my DB has 603 records, each record has about 2 paragraphs of text only. To me this is not something I would consider LARGE, from a MYSQL DB aspect.

I had to increase the maximum timeout settings to 320 seconds just so I could get it to read up to about 400 records without crashing Apache. I would add LIMIT 400 to my SQL statement and it would take about 300 seconds and display the PDF file(way too long to be reasonable). If I increased the LIMIT to say 450, it would crash apache. and Apacher error was something relating to bad memory overwrite etc. So it works pretty good, for me, up until 400 records returned and during the render phase, it would be slow and crash at around 450 records.

this seems to be the case as I googled and found many people with similar issues. It would seem that this would be something DOMPDF would know about and mention in their FAQ or fix, but they have not. So although I still would love a PDF option. I opted to simply allow the user to save data as a text file. this was very simple PHP coding (5 lines) and saves my data as a file instantly.

here is the meat of the code that saves data to a text file:

$myFile = "pdjentries.txt";
$fo = fopen($myFile, 'w') or die("can't open file");

            $data = "ALL Your Entries from PersonalDreamJournal.com\r    \n\r\n\r\n";

           while ($row = $Recordset1->fetch_assoc() )
            { 

                 $data .= date("m-d-Y", strtotime($row['date'])).'          '.$row['flag']. "\r\n";                       
                 $data .= $row['text']. "\r"; 
                 $data .= "****************************************************************";
                 $data  .= "\r\n\r\n\r\n\r\n\r\n";                  ;

            } //end while
                    $data .='----END OF ALL ENTRIES-----';


header('Content-type: application/txt');
header('Content-Disposition: attachment; filename="pdj-entries.txt"');
echo $data;

Upvotes: 0

Views: 397

Answers (1)

JRP
JRP

Reputation: 989

You could send the info to the client and then render the pdf client-side with jsPDF. It's very easy to use.

Upvotes: 1

Related Questions