converting the content of a php page to html page - php

I want to convert the content of a php file (this file is generated using some query from mysql database along with some image)to html file in order to create a pdf format. I tried converting php file to pdf but could not be succeeded. Kindly help with very short example as I am very new to the area.Thanks in advance.

you can generate a static html file from a url (to the php page) like so:
file_put_contents('static.html', file_get_contents('http://example.com/dynamic.php'));
Though writing the file to disk is probably unnecessary.
Probably your pdf function takes an html sting, in which case include and output buffering might be a suitable solution:
ob_start();
include 'dynamic.php';
$html = ob_get_clean();
create_pdf('mypdf.php', $html);
For a more specific example you would need to show your current code

Related

dompdf and php (mysql data)

I would like to pull data from a mysql db.
This data is then inserted into a html file which is then converted to a pdf using dompdf.
The template is perfect and display's well when I run call dompdf.
However as soon as I try and insert php code, the template still shows perfectly, how ever the php code is displays nothing. If I open the page its shows, so I know it works.
In the options file I have done this :
private $isPhpEnabled = true;
my php file to call the template (LeaseBase.php):
<?php
$options = new Options(); $options->set('isPhpEnabled','true');
$leasefile = file_get_contents("Leases/LeaseBase.php");
$dompdf = new Dompdf($options); $dompdf->loadHtml($leasefile);
$dompdf->stream(); $output = $dompdf->output(); file_put_contents('Leases/NewLeases.pdf', $output);
?>
I also can't seem to pick up anything in the log files.
Any assistance is appreciated
However as soon as I try and insert php code, the template still shows
perfectly, how ever the php code is displays nothing.
Answer: It shows nothing because when a php page is executed, it outputs html (and not the php code). If you don't have an echo or print or any code that generates html code from the php script, the page will in fact be blank.
It's important to remember that php is serverside code WHICH CAN generate html code as long as you instruct it accordingly.
With versions of Dompdf prior to 0.6.1 you could load a PHP document and the PHP would be processed prior to rendering. Starting with version 0.6.1 Dompdf no longer parses PHP at run time. This means that if you have a PHP-based document you have to pre-render it to HTML, which does not happen when using file_get_contents().
You have two options:
First: Use output buffering to capture the rendered PHP.
ob_start();
require "Leases/LeaseBase.php";
$leasefile = ob_get_contents();
ob_end_clean();
Second: Fetch the PHP file via your web server:
$leasefile = file_get_contents('http://example.com/Leases/Leasebase.php');
...though, actually, if I were loading the file into a variable and feeding it to dompdf without doing any further manipulation I would use dompdf to fetch the file instead. In this way you are less likely to have to deal with external resource (images, stylesheets) reference problems:
$dompdf->load_html_file('http://example.com/Leases/Leasebase.php');

Output buffering alternative php

I'm trying to save the content of a file into a PDF using html2pdf, but the file has some PHP codes which need to be processed. I made some research and I found out that I had to use output buffering so that the PHP content in the file can be processed. So I did something like:
<?php
require_once('html2pdf.class.php');
ob_start();
require_once('my_file.php');
$content = ob_get_clean();
// force download of $content to a PDF
$html2pdf = new HTML2PDF('P','A3','fr', false, 'ISO-8859-1');
$html2pdf->writeHTML($content);
$html2pdf->Output('file_name.pdf', 'D');
?>
The file my_file.php is the file that has some PHP code and the HTML content that I wanna save to a PDF, and the variable $content is the actual content with the PHP codes processed and everything. This works fine on Apache, but not on IIS.
Does anybody know an alternative way to make this work witout using ouput buffering? I tried file_get_contents('my_file.php'); but my php contents in my_file.php do not get processed when I do so.
Please, I'm looking for ways to do this without output buffering so that it can work on any server. I'm not looking for answers telling me to change my IIS server configuration or to use something else other than html2pdf.
Thanks in advance for any help
If you can modify the contents of my_file.php, you can put all the text into a variable there instead of outputting it directly.
You can use PHP/PDF Library http://php.net/manual/en/book.pdf.php
And follow this example : http://php.net/manual/en/pdf.examples-basic.php
Hope that helps :)
The easiest approach would be to edit my_file.php so that rather than containing HTML it assigns the HTML content to a PHP variable. Then all you need to do is echo the variable.
//other PHP processing goes here, or anywhere else.
$someVar = "hello world";
$myHTML = "<html>My output: $someVar </html>";
echo $myHTML;
It's an ugly way of handling HTML output, and I'm not saying it's good programming, but if you want to avoid editing config files it would be quick and easy.

Display .txt file contents in a php template?

So I'm making a notepad app in PHP, but I want to add the ability to share the file amongst your peers or something.
It's based on AJAX, and it saves the file automatically, and the file is named to what your IP address is after being hashed in md5.
What I want to do is maybe go to /view/837ec5754f503cfaaee0929fd48974e7, while the actual text file is located at /notes/837ec5754f503cfaaee0929fd48974e7.txt
I know I'll have to use file_get_contents(), but I don't know how to display it on a page.
I could just have it link to the .txt file, but I don't want it raw. I want it to have some style.
How would I go about doing this? Where can I start?
First you would need a way to store a variable in the URL (the file name). This can be easiest done using the querystring.
So the link to a file for your user to see would be '/view/?file=MYFILENAME'
This would then be interpreted by your php (this could also be wrapped in AJAXy goodness) into a path to retrieve the text file from.
view/index.php
//Fetch the file based on the get variable
//Note the relative path
$file = file_get_contents('../notes/'.$_GET['file'].'.txt');
//Print the file. You can also dress it up or wrap it in HTML tags
echo $file;
When displaying the text file, there is some built in functions that will help. Most notable nl2br() which takes the new line characters in a text file and makes them into html <br> tags.
More reading on the GET array can be found here

file_get_contents not extracting full file contents

I m using the php function file_get_contents to parse a php file. But it seems that as soon as it is reading the php tags the file_get_contents is malfunctioning.
I checked the function with a normal text file, its functioning perfectly. But even if it finds php tags in a text file, the file is being half read. How can i find a way to get the full contents.
Is the file local? Or are you trying to get a remote file? How did you check that the content is not read? Echoing it to a browser might trick you because of the < char in <?php
Use htmlspecialchars or <pre> to view the whole text. Or just look at the source of the page.

convert php file to pdf using mpdf

I can convert html page to pdf and send it via email with out any problem, but I am facing trouble with converting php file to pdf.
is it possible to convert php file to pdf using mpdf or do I need to use some other php class for this?
Thanks!
The option seems to be like, first convert the output of the php file to html file, save it and pass that file to mpdf.
Thought my solution may seem other way round but it worked well for me.
Or otherwise this Link
<?php
$file = '/home/user/Desktop/myfile.html';
$result = file_get_contents("url/of/ur/page");
echo $result; //view source now
file_put_contents($file, $result);
?>
now u can pass this file to mpdf. Realie sorie bt i havnt used mpdf till date. May be this solution works for you.
Also, other option is curl.

Categories