Log in

View Full Version : submit div content to html2pdf page



keithboesker
03-29-2013, 07:55 PM
I'm Needing to submit div content from one page to my html2pdf conversions page. I was wanting to do this using jquery so I do not have to leave my home page to make downloadable PDF file. I found an online example but I can not seem to make it work right. Any help with this project would be appreciated greatly. Here's the code I found. I just want to pass my div content so I can create a downloadable PDF file. I don't need any error message return link this script contains. Thanks



home.php



<! DOCTYPE>
<html>
<head>
<title></ title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
src="js/jquery-1.9.1.min.js" <script type="text/javascript"> </ script>
<script type="text/javascript">
$ (Document). Ready (function () {
$ ("#exportentry"). Click (function (e) {
e.preventDefault ();
if (submitted) {
var data = $ ("#container"). html ();
var filename = "Entry_Report.pdf";
$. Ajax ({
type: "POST",
url: "convert.php",
data: {data: data, filename: filename}
dataType: "json",
success: function (data) {
alert ('success');
}
})
Else {}
alert ("No Report To Export");
}
});
});
</ Script>

</ Head>
<body>
<div id="container">
<center> <h3> Nations and Flags </ h3> </ center>
<table border="1" width="500" cellspacing="0" cellpadding="2" align="center">
<tr> <td> <% = Request.QueryString ("v")%> India </ td> <td width="200"> <img src = "imag / in-t.jpg" width = "48" height = "32"> </ td> </ tr>
<tr> <td> <% = Request.QueryString ("v2")%> Australia </ td> <td width="200"> <img src = "imag / as-t.jpg" width = "48" height = "32"> </ td> </ tr>
<tr> <td> Canada </ td> <td width="200"> <img src="imag/ca-t.jpg" width="48" height="32px" </ td> </ tr >
<tr> <td> China </ td> <td width="200"> <img src="imag/ch-t.jpg" width="48" height="32px" </ td> </ tr >
<tr> <td> Germany </ td> <td width="200"> <img src="imag/de-t.jpg" width="48" height="32px" </ td> </ tr >
<tr> <td> France </ td> <td width="200"> <img src="imag/fr-t.jpg" width="48" height="32px" </ td> </ tr >
<tr> <td> United Kingdom </ td> <td width="200"> <img src="imag/uk-t.jpg" width="48" height="32px" </ td> </ tr>
<tr> <td> United States of America </ td> <td width="200"> <img src="imag/us-t.jpg" width="48" height="32px" </ td> </ tr>
</ Table>

<table border="0" width="500" cellspacing="0" cellpadding="2" align="center">
<tr> <td align="center"> <a href = " http://www.scriptarticle.com "target =" _blank "> http://www.scriptarticle.com </ a> </ td> < / tr>
</ Table>
</ Div>
<form method="post" id="exportentry" action="home.php">
<input type="submit" value="Create PDF">
</ Form>
</ Body>
</ Html>




convert.php



<? Php

require ('html2fpdf.php');

if (isset ($ _POST ['data'])) {
Urlcontents $ = $ _POST ['data'];
$ Filename = $ _POST ['filename'];
$ Date = $ _POST ['date'];
convert ($ urlcontents, $ filename, $ date);
}

function convert ($ contents, $ name, $ currdate) {
$ Pdf = new HTML2FPDF ();
$ Pdf-> AddPage ();
$ Pdf-> SetFont ('Arial', 'B', 16);
$ Pdf-> Cell (40.10, "Report Entry");
$ Pdf-> SetFont ('Arial','', 12);
$ Pdf-> Cell (90.12, '-'. $ Currdate);
$ Content = strip_tags ($ contents, '<html> <body> <meta> <img> <h2> <h4> <div> <ul> <li> <span>');
$ Pdf-> Sety (20);
$ Pdf-> WriteHTML ($ contents);
$ Content = $ pdf-> Output ('', true);
$ File = fopen ($ filename, "D");
fwrite ($ file, $ content);
fclose ($ file);
}
?>

sshss
04-05-2013, 10:09 AM
your website is on what script ? maybe you can use any ready plugin

look for something like ready html2pdf api

jscheuer1
04-05-2013, 11:12 AM
The jQuery code you have in your post has capital letters in a number of places where lower case must be used, and the external script tag for jquery is wrong. The src attribute needs to be in the opening <script . . . part of the tag. Using jQuery 1.9+ could present problems as it dropped a number of commonly supported commands from previous versions.

There are also some unnecessary spaces and other capital letters in the script code and in HTML, some or none of which might be a problem. And I question the need for json as a data type (might be good, but could be a problem). The submitted variable is undefined, so the ajax code would never fire. The form should execute on submit, not on click. The DOCTYPE is invalid as is some of the HTML, even for quirks mode.

Taking all of that together, and correcting two syntax errors in the jQuery code, I've updated the code and HTML for your sending page:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript">
jQuery(function($){ //document ready
$("#exportentry").submit(function(e){
e.preventDefault();
var data = $("#container").html();
var filename = "Entry_Report.pdf";
if(data){
$.ajax({
type: "POST",
url: "convert.php",
data: {data: data, filename: filename, date: document.lastModified},
dataType: "json", //try it without this if there's still a problem
success: function(data){
alert('success'); //to stop the success alert, remove or comment out this line
}
});
}
else {alert("No Report To Export");}
});
});
</script>

</head>
<body>
<div id="container">
<center> <h3> Nations and Flags </h3> </center>
<table border="1" width="500" cellspacing="0" cellpadding="2" align="center">
<tr> <td> <?php echo isset($_GET['v'])? $_GET['v'] : ''; ?> India </td> <td width="200"> <img src="images/in-t.png" width="48" height="32"> </td> </tr>
<tr> <td> <?php echo isset($_GET['v2'])? $_GET['v2'] : ''; ?> Australia </td> <td width="200"> <img src="imag/as-t.jpg" width="48" height="32"> </td> </tr>
<tr> <td> Canada </td> <td width="200"> <img src="imag/ca-t.jpg" width="48" height="32"> </td> </tr>
<tr> <td> China </td> <td width="200"> <img src="imag/ch-t.jpg" width="48" height="32"> </td> </tr>
<tr> <td> Germany </td> <td width="200"> <img src="imag/de-t.jpg" width="48" height="32"> </td> </tr>
<tr> <td> France </td> <td width="200"> <img src="imag/fr-t.jpg" width="48" height="32"> </td> </tr>
<tr> <td> United Kingdom </td> <td width="200"> <img src="imag/uk-t.jpg" width="48" height="32"> </td> </tr>
<tr> <td> United States of America </td> <td width="200"> <img src="imag/us-t.jpg" width="48" height="32"> </td> </tr>
</table>

<table border="0" width="500" cellspacing="0" cellpadding="2" align="center">
<tr> <td align="center"> <a href="http://www.scriptarticle.com" target="_blank"> http://www.scriptarticle.com </a> </td> </tr>
</table>
</div>
<form method="post" id="exportentry" action="home.php">
<input type="submit" value="Create PDF">
</form>
</body>
</html>

It looks like you have similar problems, at least as far as unnecessary spaces and possibly upper and lower case usage issues as well in the PHP code of the receiving page. But I'm not as well versed in that. Perhaps someone else can clean it up. It could also have syntax errors.

I will try testing it out though and let you know if I come up with anything.

I did test it and the convert.php file is a mess. Once fixed, I found that on PHP 5.3+ there were problems with the html2fpdf.php file and its helper file, fpdf.php. These would not be an issue if you are running PHP 4 or PHP < 5.3.

Here's the updated convert.php file:


<?php

require ('html2fpdf.php');

if (isset ($_POST['data'])) {
$urlcontents = $_POST ['data'];
$filename = $_POST ['filename'];
$date = isset ($_POST ['date'])? $_POST ['date']: '5/25/92';
convert ($urlcontents, $filename, $date);
}

function convert ($contents, $name, $currdate) {
$pdf = new HTML2FPDF ();
$pdf-> AddPage ();
$pdf-> SetFont ('Arial', 'B', 14);
$pdf-> Cell (120, 10, "Report Entry - $currdate");
$pdf-> SetFont ('Arial','', 12);
$pdf-> Sety (20);
$pdf-> WriteHTML ($contents);
$pdf-> Output ($name, 'F');
}
?>

I used the html2fpdf.php file and its helper files from here:

http://sourceforge.net/projects/html2fpdf/

That's version 3.0.2b (listed on the download page, version 3.0(beta) listed in the file itself) - If you have another version, results could vary. And like I say, if you have PHP 5.3 or greater, the html2fpdf.php file and its helper file, fpdf.php need a little editing - let me know.

jscheuer1
04-06-2013, 02:53 AM
Wait a minute - Do you even have PHP? These:



<tr> <td> <% = Request.QueryString ("v")%> India </ td> <td width="200"> <img src = "imag / in-t.jpg" width = "48" height = "32"> </ td> </ tr>
<tr> <td> <% = Request.QueryString ("v2")%> Australia </ td> <td width="200"> <img src = "imag / as-t.jpg" width = "48" height = "32"> </ td> </ tr>


imply that your server is asp based. Is it?

round
04-11-2013, 04:37 AM
Instade of using html2PDF use mPDF.
mPDF provide large variety of functions like background image, borders, fonts, etc..
Check the demo examples at http://www.olivettorestaurante.com.br/mpdf/examples. It might be helpful to you.

Beverleyh
04-11-2013, 05:28 AM
round, the website link you provided does not appear to be the source website and its inclusion is very confusing out of context.

mPDF may indeed prove to be useful but it would be more beneficial to people reading his thread, and more courteous to the developer, to link to the source/developer's website, which is here: http://www.mpdf1.com/mpdf/index.php

Official documentation is here: http://mpdf1.com/manual/

And official usage examples are here: http://www.mpdf1.com/mpdf/examples