Jump to content

Assistance wanted - Convert HTML output to PDF


gordonisnz
 Share

Recommended Posts


Hello.  I'm using PHP Smarty, and have generated HTML output. Now i'm trying to convert this into a PDF. (I've got a  regular website up & running - just creating monthly reports in PDF).

I've found dompdf  and set it up, & have all the files correct, However I'm getting an error:-

$sm="/home/users/MYPATYH/usr/dompdf/dompdf/vendor/dompdf/dompdf/lib";
ini_set('include_path',ini_get('include_path').":$sm");

Fatal error: Uncaught Error: Class 'DOMPDF' not found in 

Question:- WHICH file exactly, should be included in the directory, What file is being accessed for "DOMPDF"?

can someone recommend a good PDF converter (I've got all the HTML output). with a step-by-step guide as to how to install it.


1) Download files
2) Upload files 
3) find directory /usr/path/etc../ etc...

4) include "path to file" (this should be the path to file.php file... - (Exact filename its looking for)

My current code is:-

require ("/home/users/MYUSERPATH/usr/dompdf/dompdf/autoload.inc.php");
require ("/home/users/MYUSERPATH/usr/dompdf/dompdf/vendor/autoload.php");

$filename = "$file2/report-$dt.pdf";
$dompdf = new DOMPDF();
$dompdf->load_html($output);
$dompdf->set_paper('a4', 'portrait');
$dompdf->render();
file_put_contents($filename, $dompdf->output());

ERROR- Fatal error: Uncaught Error: Class 'DOMPDF' not found in ......
 

Link to comment
Share on other sites

Thanks - I'll try it - HOWEVER i don't want to learn a complete new language (if i can)...  my HTML templates already have the fonts etc all sorted.. 

I guess using this PDF - i need to set all the sizes/fonts etc manually 

Is there anything that will just convert HTMLoutput to PDF?

Link to comment
Share on other sites

Don't know.  But you could google it.

FPDF is a tool written to use php and a set of its own functions that generate cells that you define the size of and you can locate using an x/y grid on a page. Doesn't have a lot of font choices so if you have some fancy html you're gonna have to think about it.

Link to comment
Share on other sites

If you're open to using a non-PHP solution, I find the easiest way to accomplish this task is to use NodeJS along with puppeteer. This uses an instances of chromium to generate the PDF and as such for the most part supports all the same HTML and CSS features as google chrome does.  Essentially you get the same output you would if you used the 'Print to PDF' feature of chrome.

I have a PHP class and simple Javascript script I've created that makes using it fairly simple.

 

Link to comment
Share on other sites

are there Any GOOD tutorials on PDFP & tables ?

http://www.fpdf.org/en/tutorial/tuto5.htm

I'm reading the above but its only got one type of data,  (i'm looking at the fancy table)

from what i see - it just extracts data from a file & inserts it into a data array. 

HOWEVER, i have data in this format:-

Array ( [1] => Array ( [cats] => 12 [kittens] => 8 ) [3] => Array ( [cats] => 3 [kittens] => 1 ) [4] => Array ( [cats] => 2 [kittens] => 1 ) )

(thats just 1 month of data, I have 4-5 months to show so far. )

I HAVE managed to get my data into the correct format. 

However I have 4 header titles & 4 columns of data in my table - However they're not aligned with each other.. 

i'm modifying the widths of the columns to try to be underneath the titles - however shouldn't they auto-align ? 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

If you are talking about producing true 'monthly reports' for people who read reports all the time, fancy is not the way to do it.  Yes - fancy looks nice on a web page but when you are showing people raw data it is best to have it in a clean simple format that makes it easy to examine and identify trends in columns of figures or whatever you are summarizing a month's worth of activity for.  FPDF does a great job of letting you query some data and then produce organized tabular (or not) reports in a black and white (with maybe some accents of color) format for people who have to drill thru them regularly.  Learning FPDF is simply a matter of laying out in your mind how you want to organize the data, recognizing the widths of the columns you will have and then creating a row of (what are called) cells wherein you place individual fields from your query.

 

1 - do a query to get 5 columns of data

2 - plan how wide each col needs to be

3 - begin a loop on the query results

Then

while($row = $qrslts(PDO::FETCH_ASSOC))
{
	echo "$pdf->cell($col1_w, $cell_h, {$row['col1'], 1, 0, 'L');
	echo "$pdf->cell($col2_w, $cell_h, {$row['col2'], 1, 0, 'C');
	echo "$pdf->cell($col3_w, $cell_h, {$row['col3'], 1, 0, 'C');
	echo "$pdf->cell($col4_w, $cell_h, {$row['col4'], 1, 0, 'R');
	echo "$pdf->cell($col5_w, $cell_h, {$row['col5'], 1, 1, 'R');  // jump to new line
}
	

This would put your query results into a well organized table structure having no borders.  You can easily add the borders and modify the column widths by altering the values I have used.  If you have data that is bigger than a simple column FPDF offers a 'mutlicell' function to allow the data to overflow into multiple rows.

Takes a bit of practice but as you can see there really isn't a lot to it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.