www.webdeveloper.com
Results 1 to 4 of 4

Thread: create pdf of div output

  1. #1
    Join Date
    Jan 2013
    Posts
    2

    create pdf of 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. #2
    Join Date
    Mar 2009
    Posts
    495
    This really doesn't seem like a javascript question. But since you are using a Linux server it probably has the Image Magic 'convert' program available that you can use. 'convert whatever.txt whatever.pdf' will convert a text file to a pdf.

    You might try using that.

  3. #3
    Join Date
    Jan 2013
    Posts
    2
    Quote Originally Posted by Tcobb View Post
    This really doesn't seem like a javascript question. But since you are using a Linux server it probably has the Image Magic 'convert' program available that you can use. 'convert whatever.txt whatever.pdf' will convert a text file to a pdf.

    You might try using that.
    This uses mysql/css/php/javascript. I figured I'd try asking here first because it does use ajax. That's what makes it confusing because I'm not trying to output the whole page, just the ajax output that occurs within the "specs" div.

    Plus this is a web application, meant for people to use who don't have access to "convert", although I'm sure there's all kinds of unix pipes to impliment convert within the script, dompdf seems to be the best solution, but I just can't figure out the syntax. It also needs to be compatible with the CSS. The problem with outputing web pages to pdf using something like convert is that it won't catch the CSS and won't ouptut correctly.

  4. #4
    Join Date
    Apr 2013
    Posts
    26
    you can try some third party softwares to make things much eaiser, i am using a pdf processing dll to generate pdf files , i think you can have a try.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles