How to generate pdf with defined forms in php? - php

I'm php newbie. I'll try to explain my needs.
I would like to fill few forms in php that would be placed at appropriate locations in the document and then generate pdf file.
I'm using mPDF atm. because I need polish signs to show up correctly.
If you could help me anyhow I would appreciate that a lot. Peace.
include('mpdf/mpdf.php');
$html = file_get_contents("generated.html");
$mpdf=new mPDF('iso-8859-2');
$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='ISO-8859-2';
$mpdf->WriteHTML($html);
$mpdf->Output("my_file.pdf","F");
exit;
That's for generating pdf from html, but now I would like to somehow generate the html with forms that I could define first, and then generate pdf.
So far I'm thinking about form for generating html like:
<?php
$test1 = $_POST['test1'];
$test2 = $_POST['test2'];
$test3 = $_POST['test3'];
$data = "$test1 | $test2 | $test3\n";
$fh = fopen("generated.html", "a");
fwrite($fh, $data);
fclose($fh);
print "Finished";
?>
<body>
<form name="form1" method="post" action="generate_html.php">
test1:
<input type="text" name="test1">
<br>
test2:
<input type="text" name="test2">
<br>
test3:
<select name="test3">
<option value="test3_1">test3_1</option>
<option value="test3_2">test3_2</option>
<option value="test3_3">test3_3</option>
</select>
<br>
<input type="submit" name="Submit" value="Generate">
</form>
</body>
EDIT
Thanks to #Gavin
<?php
include('mpdf/mpdf.php');
$html = file_get_contents("2.html");
$html = str_replace("%title_placeholder%", $_POST['title'], $html);
$mpdf=new mPDF('iso-8859-2');
$mpdf->allow_charset_conversion=true;
$mpdf->charset_in='ISO-8859-2';
$mpdf->WriteHTML($html);
$mpdf->Output("3.pdf","F");
?>
<body>
<form name="form1" method="post" action="1.php">
Title: <input type="text" name="title">
<br>
<input type="submit" name="Submit" value="Generate">
</form>
</body>
Works great,thanks!

You are loading the contents of a static HTML file so you could put place holders within the html...
<h1>%title_placeholder%</h1>
and then use file get_contents
$html = file_get_contents("my_file.html");
and replace the placeholders with your form data
$html = str_replace("%title_placeholder%", $_POST['title'], $html);
then write your new string to mPDF

Related

create html file from form output parsed by php script

I want to make an html document update with input from a form
The goal is to be able to enter the URL, enter the description, enter the date created and output to a file.
My thoughts are to break the HTML document into pieces, begin.txt newarticle.txt and end.txt
Then piece it back together using fopen and fwrite.
I'm sure there's an easier way, but this is how I'm currently trying to do it.
<html>
<body bgcolor="#FFFFFF>
<H1>Add new Article</h1>
<form action="newarticle.php" method="post">
Paste the link address
<input type="text" name="url">
</br>
Paste the description:
<input type="text" name="description">
</br>
Paste the date article was released:
<input type="text" name="date">
<p>
<input type=submit value="Create Article">
</form>
</body>
</html>
newarticle.php
<?php
$v1 = $_POST["url"]; //You have to get the form data
$v2 = $_POST["description"];
$v3 = $_POST["date"];
$file = fopen('newarticle.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
$content = $v1. PHP_EOL .$v2. PHP_EOL .$v3;
fwrite($file , $content); //Now lets write it in there
fclose($file ); //Finally close our .txt
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>
This gives me the output on three separate lines.
How do I have it create a file with content formatted it into an actual piece of code :
<li><a href=$v1>$v2</a></li>
<li>$v3</li>
If you don't mind the format of the html always being exactly the same with the same set of elements, but different attribute values and inner HTML, you can use a heredoc to build up the html. Basically a multi-line string. For example:
$v1 = "info from the form";
$v2 = "more info!";
$built = <<<EOF
<li>$v1</li>\n
<li>$v2</li>
EOF;
echo $built;
this will output:
<li>info from the form</li>
<li>more info!</li>

How can I get the output for form without the page changing

I am trying to get information from textarea to be converted to a .py file.
Here's my file https://jsfiddle.net/girlwhocancode/68mb49gq/
<form action=action.php method="post">
<center>
<textarea placeholder="Code you want to execute in python..."></textarea>
</center><br/>
<center><input type="submit" class="button_example"></center>
</form>
and this is my php file:
<?php
$path = #PATH;
if (isset($_POST['field1'])) {
$fh = fopen($path,"a+");
$string = $_POST['field1'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
For some reason my php isn't working and I have no idea how to keep the same webpage after submitting
Try this:
<form action="action.php" method="post">
<center>
<textarea name="script" placeholder="Code you want to execute in python..."></textarea> //added name
</center><br/>
<center><input name="submit" type="submit" class="button_example"></center>
</form> // added name submit
in php
<?php
$path = #PATH;
if (isset($_POST['submit'])) {
$fh = fopen($path,"a+");
$string = $_POST['script'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Remember that in php $_POST/$_GET keys are always your form field
names

PHP SELF is automatically called on page load

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$fn=$_POST["filename"];
$content=$_POST["txt"];
$fp= fopen("abc.txt", "w") or die("unable to open file");
fclose($fp);
}
?>
<html>
<head>
<title>Files</title>
</head>
<body>
<center>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post" id="show">
<input type="text" name="filename" placeholder="File Name"><br>
<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>
<input id="ixtbtn" type="submit" id="tabuttton" value="Done">
</form>
</center>
</body>
</html>
Php is automatically getting called on page load. But I need it to be called when submit button is pressed.
<?php
if (isset($_POST['submitted']))
{
$fn=$_POST["filename"];
$content=$_POST["txt"];
$fp= fopen("abc.txt", "w") or die("unable to open file");
fclose($fp);
}
?>
<html>
<head>
<title>Files</title>
</head>
<body>
<center>
<?php echo "<form action='" . htmlspecialchars(#_SERVER['PHP_SELF']) . "' method='post' id='show'>"; ?>
<input type="text" name="filename" placeholder="File Name"><br>
<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>
<input id="ixtbtn" type="submit" id="tabuttton" value="Done" name='submitted'>
</form>
</center>
</body>
</html>
Played with your code a bit and noticed two things: (1) your statement is outside your php code block and in straight html. (2) On my system (Firefox or Safari on OSX) that meant that the php $_SERVER['PHP_SELF'] was never translated back to php, it was treated as text and the post command kept posting to the server looking for a URL starting with the php If I use echo inside php to output your HTML, the variables ($_SERVER) are not substituted and go in as straight text. So I put your html into a string variable first to translate the variables and then echoed your html. Saved the file as an PHP file instead of as an html file and your code worked just fine. So, four net changes.
Move the closing php statement (?>) to the end of the file
copy the HTML into a php string
$outputString = "";
$outputString = $outputString.'<!DOCTYPE html>';
$outputString = $outputString.'<html><head><meta charset="utf-8"><title>Files</title>';
$outputString = $outputString.'</head><body><center>';
$outputString = $outputString.'<form action="'.$_SERVER['PHP_SELF'].'" method="post" id="show">';
$outputString = $outputString.'<input type="text" name="filename" placeholder="File Name"><br>';
$outputString = $outputString.'<textarea name="txt" id="txtarea" id="txtarea" rows="20" cols="50" placeholder="Text goes here..."></textarea><br>';
$outputString = $outputString.'<input id="ixtbtn" type="submit" id="tabuttton" name="myButton" value="Done">';
$outputString = $outputString.'</form></center></body></html>';
Echo the string
Save as .php instead of .html

How to fill a TextArea with HTML code inside a form

So I built a site last night and now I want to add in the functionality of being able to edit the files from the web. To do a proof of concept I created:
A list box filed with file names in the DIR (Working)
A submit button that calls the selected file (Working)
A text area to display the code (Working kinda)
A save button (not hooked up yet)
I can't figure out how to fill the text area with a HTML file that contains <form> tags. As soon as I do it breaks the actual form on the page and messes up all the ui. This is just a prototype but I can only imagine the real site will have a similar issue.
Here is the relevant code for your testing purposes.
<html>
<body>
<form action="getdir.php" method="GET">
<table>
<td valign="top">
<select name="file">
<option value=""></option>
<?php
$size = 0;
foreach(glob("*.html") as $filename)
{
$size++;
}
$count = 0;
$files[size];
foreach(glob("*.html") as $filename)
{
$files[$count] = $filename;
print('<option value=');
print($filename);
print('>');
print($filename);
print('</option>');
$count++;
}
?>
</select>
<input type="submit" value="Get Code"/></td><td>
</form>
<form action="getdir.php" method="POST">
<?php
$f = $_GET['file'];
if($f!=null){
$openedFile = fopen($f,'r');
$read = fread($openedFile,filesize($f));
print('<textarea name="tb1" rows="100" cols="100">');
print(addslashes($read));
print('</textarea></td>');
fclose($openedFile);
}
else{
print('<textarea name="tb1" rows="100" cols="100"></textarea></td>');
}
?>
<td>
<input type="submit" style="height:800px" value="Save Code"/>
</td>
</form>
</body>
</html>
Try replace addslashes with htmlspecialchars

Edit XML via HTML Form (PHP)

I'm trying to create a form that will let you edit the contents of an xml tag. i currently have a form.php:
<?php
$data=simplexml_load_file('welcome.xml');
$welcome=$data->item->name;
?>
<form method="post">
<textarea name="name"><?php echo $welcome ?></textarea>
<br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('welcome.xml');
$data->item->name=$_POST['name'];
$handle=fopen("welcome.xml","wb");
fwrite($handle,$xml->asXML());
fclose($handle);
}
?>
and welcome.xml:
<welcome>
<item>
<name>$welcome</name>
</item>
</welcome>
when i press submit it doesn't save what's entered, it just refreshes the page and deletes whatever the value in the xml file was before..
UPDATE
The form works now, but I've added a reset button, i need it to clear the xml file so it only has the <welcome> tags. I've changed $data->item->name=$_POST['welcome']; to $data=''; but it deletes the text and keeps the tags still.
You can do it with simplexml.
To read data from xml:
$data = simplexml_load_file('welcome.xml');
$welcome = $data->item[0]->name;
And to write data:
$data = simplexml_load_file('welcome.xml');
$data->item[0]->name = $_POST['welcome'];
$handle = fopen("welcome.xml", "wb");
fwrite($handle, $xml->asXML());
fclose($handle);
EDIT:
For the question in the comment:
<?php
if(isset($_POST['submit'])) {
$data=simplexml_load_file('welcome.xml');
$data->item->name=$_POST['name'];
$handle=fopen("welcome.xml","wb");
fwrite($handle,$data->asXML());
fclose($handle);
}
$data=simplexml_load_file('welcome.xml');
$welcome=$data->item->name;
?>
<form method="post">
<textarea name="name"><?php echo $welcome ?></textarea>
<br>
<input type="submit" name="submit" value="submit">
</form>

Categories