I want to get the contents of a php file in HTML. I am using it as a HTML email template but it needs to be populated with dynamic data from my database.
I tried
file_get_contents('/emails/orderconfirm.php');
But all that does is return the php code before it is processed by the server into HTML.
I'm guessing it's really easy? any ideas would be much appreciated.
Thanks
In order to execute the specific code in your emails/orderconfirm.php, you need to include its content.
Included content are going to be compiled and rendered.
However, You need to get the content as long as you echo
That way, you need to use ob_start() library.
Here's an example of usage :
ob_start(); //Init the output buffering
include('emails/orderconfirm.php'); //Include (and compiles) the given file
$emailContent = ob_get_clean(); //Get the buffer and erase it
The compiled content is now stored in your $emailContent variable and you can use it the way you want.
$email->body = $emailContent;
ob_start();
require 'emails/orderconfirm.php';
$email = ob_get_clean();
http://php.net/ob_start
You are close, insert the complete URL
file_get_contents('http://example.com/emails/orderconfirm.php');
Try going to the full http address eg:
file_get_contents('http://mydomain.com/emails/orderconfirm.php');
Related
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');
i am building my own API monitoring tool and i want to know if it's possible to save the response that the PHP script wanted to be monitored will generate to a variable to use it in other stuff after script code process complete, something similar to ob_start(); in the top of the script then
$output = ob_get_contents();
ob_end_clean();
in the bottom and then do what i want with $output
is this the best way to do it ?
figured it out if i use ob_get_contents(); without requesting ob_start(); it will show main output buffer value
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.
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
I am having a php file which executes some code to generate a html file.Its like I m having a form from which some data will be posted to x.php file, which gives a output(a webpage). I want to save that output in a html file.What the most efficient way of doing this.?
EDIT
I want to save it on sever side. Actually the thing is i want to create pdf file for that.. I wrote everything else.Now the thing is i want to save the output in a html page.So that i can convert it into pdf file..
Try something like this:
// Start output buffering
ob_start();
// run code in x.php file
// ...
// saving captured output to file
file_put_contents('filename.htm', ob_get_contents());
// end buffering and displaying page
ob_end_flush();
If you cannot use the ob_* functions, you can also write the form to a variable and then save that variable.
Look at ob functions (see http://php.net/manual/en/function.ob-start.php) that allows you to capture every output (echo, print, etc...) from the page.
using ob_* functions such as ob_get_contents(), a php script can catch it's output.
probably with ob_start and output_callback see http://php.net/manual/en/function.ob-start.php