How to use wkhtmltopdf with POST data - php

I currently use wkhtmltopdf where I am attempting to generate a .pdf file after a user submits a form on our website. The form data gets submitted to post.php, where it displays nicely as a formatted web page. I want to generate a .pdf file of this exact page.
But the problem begins when trying to execute wkhtmltopdf. I get a continuous loop because I'm trying to generate the .pdf from inside of this post.php file, which is also the target.
I have also tried to include() a separate PHP file to handle the exec() function, but I still get a continous loop.
Maybe a visual below helps:
form.php which contains something like below...
<form id="ecard" action="post.php" method="post">
<input type="text" name="ecard_message" />
<input type="submit" />
</form>
post.php which holds the posted data and contains HTML like so...
<div class="content">
<?php echo $_POST['ecard_message']; ?>
</div>
<?php exec("wkhtmltopdf http://example.com/post.php ecard.pdf"); ?>
My code DOES work when the exec() function is runs separately from these files, but how would I accomplish the exec() within this same process, automatically?
Any insight is very much appreciated.

calling wkhtmltopdf http://example.com/post.php ecard.pdf will lose the post data, so even if it worked, the pdf will be empty.
Rather generate the pdf as html and then pass it to wkhtmltopdf. Eg: (untested)
<?php
$html = '<div class="content">' . $_POST['ecard_message'] . '</div>';
exec("echo $html | wkhtmltopdf - ecard.pdf");
?>
See Is there any wkhtmltopdf option to convert html text rather than file? for explanation of using text instead of files.

Related

Do you need to change an HTML documents ending to work with PHP

I've started delving into PHP and I've seen that PHP files have a ".php" ending. I was wondering how this works, if you can put HTML code into one of these files, and how one would usually use PHP to collect information, if they'd create a separate web page for the form or if they can just convert an HTML page into a PHP page.
Apologies if my question is a bit hard to understand, it's a bit hard to put to words what I want to know.
Thanks!
You can put html in php files, for example:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
This file file will display on a browser the classic Hello World page!
The way to use php depends of what you're trying to do. Sometimes it will be mixed with html, sometimes no.
Whenever a webserver sees a page ending in .php and has a PHP interpreter registered then it will pass the page to the interpreter.
The interpreter will pass any result back. If there's no PHP code in the page (e.g. there's only HTML) the interpreter will just pass the HTML back the way it gets it, but if you add a <?php ... ?> block the interpreter will evaluate the contents of that block and replace it with the result (e.g. if you do <?php echo "Hello world" ?> it will replace it with "Hello world").
That's just the short version.
PHP and HTML would be used in conjuction to create web pages. One can convert a HTML document to PHP in order to acheive more functionality as they are used for different things. PHP is a server side scripting language i.e. it facilitates communication between the user interface (the HTML & CSS) and the back-end server stuff (data). HTML is a markup language which on it's own does not communicate with the server, it is simply used to display static content on a page.
Together, they can be used to create dynamic web pages. One way to think of it is like this: HTML provides the content whereas PHP manipulates the content in order to provide functionality.
In other words, if we were building a house then HTML would be the bricks and mortar and PHP the plumbing, heating and electricity.
You can include HTML code in a PHP file however all PHP scripts must start with:
<?php
and end with
?>
Regarding your question on collecting information, PHP provides functions which enable form creation and binding to databases, take the following:
<?php
echo '<form method="post" action="">
Username: <input type="text" name="user_name" /><br/>
Password: <input type="password" name="user_pass"><br/>
Password again: <input type="password" name="user_pass_check"><br/>
E-mail: <input type="email" name="user_email">
<input type="submit" value="Register" />
</form>';
?>
This is an example of a simple form written in PHP to enable a user to log in, where HTML and PHP differ is that while you can create the same form in HTML, PHP gives you access to all sorts of nifty features such as performing data validation, security checks and database access.

PHP variable used in shell_exec giving different results

This is a repost because my question before was not clear enough and deleted it to prevent further confusion.
I have a shell script that takes one argument. It uses the argument to cURL a website which then uses grep, head and ${variable#*>} to extract some data from a website.
In terminal everything works well with the command below. $stockCode being the argument. ./priceAlert.sh $stockCode
I tried to make this into a working webpage on my local host. (MAMP osx) I tried different solutions but the website will always spit out "not from same origin" except for the shell script.
After searching for how to integrate shell scripts with web I came across PHP and now I am figuring out how to get PHP to talk to my shell script.
Structure of my project:
HTML page with an input inside a form
<form action="priceAlert.php" method="post">
<input type="text" name="stockCode"></input>
<input type="submit" name="submit" value="Submit me!">
</form>
A PHP page that receives the input from the HTML page via POST
$stockCode2 = $_POST['stockCode'];
echo $stockCode2;
$output = shell_exec('sh priceAlert2.sh ' . $stockCode2);
echo "<pre>$output</pre>";
Now the interesting part...
The value of $stockCode2 from the HTML form is 033360.
If I use $stockCode2 as POST, the shell script extracts the part of the website that I do not want. (The code above extracts the part I do not want)
If I hard code the value, 033360, into $stockCode2 the shell script extracts the part I want. The code below extracts the part of the website I do want.
$stockCode2 = '033360';
$output = shell_exec('sh priceAlert2.sh ' . $stockCode2);
echo "<pre>$output</pre>";
If I echo everything they all show the value 033360 but behave differently.
Is there a way to fix this? Could this be a way for the target site to prevent scraping?
Thank you.

Setting HTML Attribute via PHP File

I'm new to PHP and seem to have ran into a problem I can't seem to get around.
I have a form on a secure page that creates a PHP file to store a text value. I named this variable $text.
The Form HTML Code:
<form action="upload_title.php" method="post">
<label for="text">Title 1:</label>
<input type="text" name="text" id="text"><br>
<input type="submit" name="submit" value="Submit">
</form>
The upload_title.php then seems to store the text input as $text in filename.php:
<?php
$var_str = var_export($_POST['text'], true);
$var = "<?php\n\n\$text = $var_str;\n\n?>";
file_put_contents('filename.php', $var);
?>
This seems to be functional as the form will generate filename.php, below is an example if I typed Store into the form input and submitted on the webpage.
<?php
$text = 'Store';
?>
Now the issue I'm encountering is not being able to retrieve this stored as a attribute in separate html document, the index.html in my case.
This was my best approach to changing the title attribute of an image:
<a href="upload/1.jpg">
<img src="upload/thumb.jpg" title="<?php include 'filename.php'; echo htmlspecialchars($text); ?>" alt="" class="image0">
</a>
This does not work, but I can see my JQuery detects that this is trying to be populated but does not extract the data from filename.php on the `index.htm' page.
Thank those in advance for your advice and insight, it is sincerely appreciated.
Your issue is probably the fact that you are using an html file instead of a php file, in this case index.html.
Your server is likely not set up by default to process .html files as php so the php does not get executed.
Apart from that it is not a very good way to store your value as when the php does get executed, you introduce a security risk and you use a lot more storage than necessary. You'd better store the value in a database or text file.

PHP related files only work when launched from dreamweaver

I am starting to learn php, I made a form that uses a php file to write the information into an xml file. The form is in an html file, and the php action in a seperate file. I am using EAsyphp 12.1
Whenever I use the upload form publishing it through dreamweaver everything goes fine.
But when I open it directly into the broswer it shows me part of the php code.
This is the code for html
<form enctype="multipart/form-data" action="chng.php" method="POST">
<div>Name <input type='text' name="tf" /> </div>
<div>image <input name="uploadedfile1" type="file" /> </div><br />
<input type="submit" />
</form>
The php is
$target_path4 = "../img/";
$target_path4 = $target_path4 . basename( $_FILES['uploadedfile4']['name']);
$name4="../img/linkimagen.jpg";
$t = $_POST['tf'];
if(move_uploaded_file($_FILES['uploadedfile4']['tmp_name'], $name4)) {
$xml = simplexml_load_file('info.xml');
$xml->names[0]=$t;
file_put_contents('info.xml', $xml->asXML());
$xml->images[0]=$shortname;
file_put_contents('info.xml', $xml->asXML());
}
?>
The xml information is then picked up by another page using Jquery ajax.
Everything works fine if I launch the form file using dreamweaver as I said before. Also, sometimes the page that displays the information from the xml will display it if opened directly from the browser, other times it won't, however it will always display if opened from dreamweaver. I've found information for php not working in dreamweaver but not the opposite, as is my case. Is there anything I have to do so it works always? Thanks for any info!
Well if you upload it to your server - there is a php parser which parses your file
if you open it local it won't get parsed so it's just another text document.
Of course you could install something like xampp, to have a local webserver but if you are just starting out - it's a bit much I guess :)
-- Edit --
An addition: Please watch the behaviour of your browser. If you have already a webserver installed on your computer it has an adress which usually is something like h ttp://localhost/ or h ttp://127.0.0.1 - if your file doesn't get parsed it's probably because it opens directly like C:\programm files\dreamweaver\whatever\index.php and not
h ttp://localhost/index.php
hope this helps you ;).

PHP Code to Insert Text Box Text Into HTML Body

I am using the code below:
<form>
<textarea cols="50" rows="4" name="link"></textarea>
<textarea cols="50" rows="4" name="notes"></textarea>
<input type="submit" value="Submit">
</form>
It creates two text boxes and I was wondering how to get them from this into a different html file's body code? I think I would need PHP.
At the end I would just want it to display a message saying "Complete" and in a html file e.g code.html in the tags it would have the contents of the two text boxes seperated by one line.
Regards!
Sounds like you would need PHP to receive the data.
See this for a basic form that posts to a PHP page.
you most use php html like this:
echo '<form id="form1" name="form1" method="post" action="">'.
'<p>PASWORD<br>'.
'<input type="text" name="name0" id="text" /><br><br>'.
'<p>BACKLINK<br>'.
'<input type="text" name="name" id="text" />'.
'<br>;'.
'<input type="submit" name="Enter" id="Enter" value="Enter" />'.
'</form>';
and use if for run html
if ($_POST['name0']>''){
your code
}
To accept input one one page and display it on another will require PHP. The general idea:
The HTML form page:
Contains you HTML form where the user enters and submits their input. The data from the form is sent to the PHP processing script. We'll assume you use the POST method.
The PHP processing script:
Contains code to read the POST values and sanitize them. This means you want to check for HTML you don't want, script tags, etc - anything you don't want. In this case, the user will be displaying this code to themselves, so they can't really harm another user. However, it's something important to keep in mind. The rule of thumb - never trust user input - always check it and clean it. Now that the PHP script has read and cleaned the data, you want to display it.
The PHP display script:
Contains the HTML of the page and PHP code to display the values you captured in the PHP processing script.
Other notes:
The PHP processing and display scripts can live in the same file. You will have to process before you display. Alternately, you can run the processing script and then include the display script. Both will work.

Categories