Avatar of Bruce Gust
Bruce GustFlag for United States of America asked on

Export a page to pdf using php

I've got a page that looks something like this:

Popup Window
The page is being generated by this:

<?php
date_default_timezone_set('America/Chicago');
require_once('model_class.php');
$display=new UpdateAdmin();
$addition=$display->update_display();
?>

<!DOCTYPE html>
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
</head>

<body style="width:100%; height:auto; margin-left:25px; margin-right:25px; font-family:arial, helvetica; font-size:10pt;"><br><br>
<?php
$body="";
$body.="<b>";
foreach($addition as $update)
{
$body.= stripslashes($update['name']);
$body.="<br>";
$body.=stripslashes($update['region']).' | '.stripslashes($update['market']);
$body.="<br>";
$body.=date("F jS, Y", strtotime($update['update_date']));
}
$body.="</b><br><hr><br><b>Significant Outages</b><br><br>";
$body.=stripslashes($update['outages']);
$body.="<br><br><b>Special Events Performance</b><br><br>";
$body.=stripslashes($update['performance']);
$body.="<br><br><b>Any FOA Updates / Special Projects Updates</b><br><br>";
$body.=stripslashes($update['updates']);
$body.="<br><br>";
echo $body;
?>
<!--https://www.youtube.com/watch?v=HLxnw4Jt_AM-->
</body>
</html>

Open in new window


There's not a whole lot to the page, but, then again, it varies depending on the kind of data my user is going to input.

That said, some of the solutions I've seen, as far as exporting HTML content into a pdf document, have you crafting cells and tables and then inserting data into those digital place holders. I may be able to get that to work, but before I started, I wanted to ensure there wasn't something even more direct.

Is there a way in which you can offer you user a button that when clicked prints the HTML page that the user's looking at? I've looked at fpdf and mpdf1 and they're pretty close, but before I start building tables and figuring out how to populate those cells with data, I wanted the eyes of someone else on my scenario just so I can be certain that I'm headed in the right direction and there isn't something even easier that I'm overlooking.

What do you think?
PHPJavaScript

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
Ray Paseur

You're on the right path.  FPDF and/or TCPDF are good tools - stable, fast.  Document composition takes some time and attention to detail, as well as some trial and error, so it's good to give yourself plenty of time-budget for these things.  Once the output is working, you can link to a page that creates the PDF dynamically.

I've posted some simple examples of these classes at E-E in the past.  If you can't find them with a search in the PHP topic area, please post back and I'll look them up and attach them here.
ASKER
Bruce Gust

Ray, since reading your answer, I've since popped the hood on fpdf and it's proven to be more than satisfactory, especially with the "writeHTML" functionality. But I've recently encountered something that has me thinking I may need to go down a different path and I wanted your eyes on it before I punt.

The graphic that you see above includes a table. It's HTML and it's not consistent. By that I mean that every user has the opportunity to format their table in ways that will include varying numbers of rows and cells along with different colors etc. So, with that being the case, although I can see how to create a dynamic pdf using fpdf, it doesn't look like I can build a pdf document that can dynamically recreate an HTML table. I can build a table and I can populate each cell dynamically, but the architecture of the table remains fixed. If the table I need to display is different in both form and substance, then I've got a problem.

I'm planning on telling my client that we can save the web content that's coming from these users, but it won't be using fpdf or anything like that for the reasons I've documented above. Rather, I'll have him consider something like PrimoPDF.

What do you think?
Ray Paseur

...it doesn't look like I can build a pdf document that can dynamically recreate an HTML table
That is an important insight that usually takes folks a long time to discover.  Glad you got to it quickly!

The central issue is this: HTML is a semantic markup language.  It is used to attach meaning to pieces of data, and it has no knowledge of the presentation of that data - browser presentation is controlled by CSS.  In contrast, PDF is a document layout language, used for precise positioning and presentation of data.  It has no knowledge of the content or meaning of that data.  HTML and PDF are almost as different as fish and bicycles.  About the only overlap is that they can both be written by PHP.

I'm not familiar with PrimoPDF, but it seems to have a lot of dependencies.  You might want to think about the design of a generic PDF that can accommodate some upper limit suitable for all of the "stuff" a client can put into the site.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
Bruce Gust

You might want to think about the design of a generic PDF that can accommodate some upper limit suitable for all of the "stuff" a client can put into the site.

I'm not certain I can and your insight, I believe, reinforces that. Most of what my users are going to be entering into this interface is RTF type stuff. They're expecting to see their tables, their fonts and their colors recreated exactly as they entered it into what I've built thus far. As far as recreating it via HTML - no problem. Exporting it into a pdf - with a conceivably different architecture and formatting at every turn - not with fpdf.

And Ray, that's not me pushing back. That's me adding some more context to see if you're still able to envision a generic pdf that accommodate text that's been written in red for one user and then another person has different text, but this time it's black and emboldened and they've included a graphic.

While I can reproduce that using fpdf as a static, I don't see it happening dynamically.

When you say "generic PDF," what do you see?
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question