Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2010
    Posts
    24
    Thanks
    8
    Thanked 0 Times in 0 Posts

    create pdf from div output

    Trying to complete a script that will make a pdf of the output that it pulls from mysql database.

    What happens is when someone puts a model into the form (index.php) it displays data about the model in the "specs" div (getspecs.php). I need a button that'll make a pdf of everything in the "specs" div.

    I've looked at verious pdf making methods, the problem is I can't figure out where to put the code in the script or the proper syntax to use.

    I just need somene familiar with this to recommend a pdf making solution and where to insert the code and the proper syntax to use to provide a button that will "create a pdf". PM if you need to see a live version. I'm on a linux server.

    index.php
    Code:
    <?php
    ob_start();
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    	<head>
    	<title>Product Specs PDF Maker - v1.0</title>
    	<link rel="stylesheet" href="styles/style.css" type="text/css">
    		<script type="text/javascript">
    			function getSpecs(str)	{
    				if (str=="")  {
    					document.getElementById("txtHint").innerHTML="";
    					return;
    				}
    				if (window.XMLHttpRequest) {
    					// code for IE7+, Firefox, Chrome, Opera, Safari
    					xmlhttp=new XMLHttpRequest();
    				}
    				else {
    					// code for IE6, IE5
    					xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    				}
    				xmlhttp.onreadystatechange=function() {
    					if (xmlhttp.readyState==4 && xmlhttp.status==200) {
    						document.getElementById("getSpecs").innerHTML=xmlhttp.responseText;
    					}
    				}
    				xmlhttp.open("GET","getspecs.php?model="+str,true);
    				xmlhttp.send();
    			}
    		</script>
    		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    	</head>
    	<body>
    		<div id="header">
    				<div id="form">
    					<form onsubmit="return false;">
    						<b>Model#:</b> <input onkeyup="getSpecs(this.value)" />
    					<input type="submit" value="Submit"/>
    					</form>
    				</div>		
    		</div>
    		<br />
    		<div id="getSpecs">
    			<div id="info">Product info listed here.</div>
    		</div>
    	</body>
    </html>
    
    <?php
    	$HtmlCode= ob_get_contents(); 
    	ob_end_flush();
    ?>
    getspecs.php
    Code:
    <?php
    require_once("pdf/dompdf/dompdf_config.inc.php");
    ?>
    
    <?php
    // table row is not echoed if value is NULL
    // table variable is not echoed if it is NULL
    	include ('link.php');
    	$model = str_replace(" ","",trim($_GET["model"]));
    	$result = mysql_query("select * from test_table where model ='$model'");
    	if ($row = mysql_fetch_object($result)) {
    		if ($row->model)
    				echo '<div id="menu">';
    				echo '</div>';
    				echo '<div class="clear"></div>';
    				echo '<div id="specs" name="specs">';
    					echo '<div id="model">';
    						echo '<span class="model">'. $row->model . '</span><br />';
    						echo '<span class="description">' . $row->description . '</span><br />';
    					echo '</div>';
    					
    					// Product Specifications
    					echo '<div id="tables">';
    
    					// Table 1
    						echo '<h3>Table 1</h3><dl>';
    						if ($row->table1_var1)
    							echo '<dt>Variable 1</dt><dd>' . $row->table1_var1 . '</dd>';
    						if ($row->table1_var2)
    							echo '<dt>Variable 2</dt><dd>' . $row->table1_var2 . '</dd>';
    						if ($row->table1_var3)
    							echo '<dt>Variable 3</dt><dd>' . $row->table1_var3 . '</dd>';
    						if ($row->table1_var4)
    							echo '<dt>Variable 4</dt><dd>' . $row->table1_var4 . '</dd>';
    						if ($row->table1_var5)
    							echo '<dt>Variable 5</dt><dd>' . $row->table1_var5 . '</dd>';
    						echo '</dl>';
    
    					// Table 2
    						echo '<h3>Table 2</h3><dl>';
    						if ($row->table2_var1)
    							echo '<dt>Variable 1</dt><dd>' . $row->table2_var1 . '</dd>';
    						if ($row->table2_var2)
    							echo '<dt>Variable 2</dt><dd>' . $row->table2_var2 . '</dd>';
    						if ($row->table2_var3)
    							echo '<dt>Variable 3</dt><dd>' . $row->table2_var3 . '</dd>';
    						if ($row->table2_var4)
    							echo '<dt>Variable 4</dt><dd>' . $row->table2_var4 . '</dd>';
    						if ($row->table2_var5)
    							echo '<dt>Variable 5</dt><dd>' . $row->table2_var5 . '</dd>';
    						echo '</dl>';
    
    					// Table 3
    						echo '<h3>Table 3</h3><dl>';
    						if ($row->table3_var1)
    							echo '<dt>Variable 1</dt><dd>' . $row->table3_var1 . '</dd>';
    						if ($row->table3_var2)
    							echo '<dt>Variable 2</dt><dd>' . $row->table3_var2 . '</dd>';
    						if ($row->table3_var3)
    							echo '<dt>Variable 3</dt><dd>' . $row->table3_var3 . '</dd>';
    						if ($row->table3_var4)
    							echo '<dt>Variable 4</dt><dd>' . $row->table3_var4 . '</dd>';
    						if ($row->table3_var5)
    							echo '<dt>Variable 5</dt><dd>' . $row->table3_var5 . '</dd>';
    						echo '</dl>';
    
    					echo '</dl>';
    
    					echo '</div>';
    
    					echo '<div id="product_img1">';
    					if ($row->product_img1)
    						echo '<img src="/images/products/' . $row->product_img1 . '" />'; 
    					echo '</div>';
    					echo '<div class="clear"></div>';
    
    					echo '<div id="product_img2">';
    					if ($row->product_img2)
    					 	echo '<img src="/images/products/' . $row->product_img2 . '" />'; 
    					echo '</div>';
    					echo '<div class="clear"></div>';
    
    					echo '<div id="product_notes">';
    					if ($row->product_notes)
    						echo  $row->product_notes ; 
    					echo '</div>';
    
    					echo '<input type="submit" name="submit" id=submit" value="Create PDF" class="pdfgen" />';
    					echo '</form>';
    
    					echo '<div class="clear"></div>';
    			echo '</div>';
    		}
    	else echo '<div id="info">Product not found. <a href='.$url.'>Create new product?</a></div>';
    	mysql_free_result($result);
    ?>

  • #2
    New Coder
    Join Date
    Jan 2013
    Location
    Sunnyvale, CA
    Posts
    97
    Thanks
    6
    Thanked 7 Times in 7 Posts
    I see noone's commented on this yet. From a high-lever perspective, if you extract the data that you want to include in the pdf, and embed it into an xml object, then you can use a transformation document to output the pdf via XSL-FO using fop.

    That should get you started...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •