HTML to PDF , Result columns are empty - php

I am using following codes to create pdf.
first memberinfo.php page contains table with name, address, city etc of member from mysql database. This file is in www.example.com/admin folder
second is member_pdf.php it it used to create PDF. It is in www.example.com/admin/do-pdf/dompdf/ folder
Code in memberinfo.php :
<?
include("../include/function.php"); // to connect database
// Create an object for an class
$bsq=new bsq();
//use class function to connect the database
$bsq->connect_db();
//=================================================================================
$reg_id=$_GET["reg_id"];
$where="reg_id='".$_GET["reg_id"]."'";
$web_dataRS=$bsq->webdreamselect('tablename',$where,'','','','');
$member_data=mysql_fetch_array($web_dataRS);
?>
//This Table Is Shown When I click On View Details Link On Member's List Page...
<table>
<tr>
<td> Name </td><td><?=$member_data['name'];?></td>
<td> Address </td><td><?=$member_data['address'];?></td>
..
..
..
</tr>
</table>
Code In member_pdf.php :
<?php
require_once("dompdf_config.inc.php");
ob_start();
//be sure this file exists, and works outside of web context etc.)
require("http://example.com/admin/memberinfo.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$dompdf->stream("file.pdf");
?>
Now problem is , PDF is created But table TDs containing $member_data['name'] , $member_data['address'] etc are empty.....
See Image =

Related

Downloading pdf file directly from database

Im trying to allow my user to download the file that they select in my system. The code i have used downloads something but it says failed forbidden. I haven't used directories to store the files but rather stored the file in a longblob data type directly in the database. If someone could please show me how to get it to download successfully I would appreciate it.
<tbody>
<?php
require_once 'dbcon.php';
$query = $conn->query("SELECT fileID, filename, file, dateuploaded from empfiles WHERE eemail = '$_SESSION[login_employee]'");
$count = 1;
while($fetch = $query->fetch_array()){
?>
<tr>
<td style="height:50px;width:100px"><?php echo $count++?></td>
<td style="height:50px;width:200px"><?php echo $fetch['filename']?></td>
<td style="height:50px;width:100px"><?php echo $fetch['dateuploaded']?></td>
?>
</td>
<td colspan="2">
<td>
echo"<a download href=\"file/{$row['file']}\">Download this file</a>";
</td>
</tr>
<?php
}
?>
</tbody>
The value of the href attribute needs to be a URL.
You can't just take a blob of PDF out of the database and coerce it into a string.
You need to either base64 encode it and then convert it to a data URL (noting that there is a good change this will result in a URL that is too long for the browser to accept).
or
Create a new webservice that extracts the PDF from the database and generates the download based on, for example, the row id. Then link to that webservice with the row id in the query string.

insert image in html table from database

I am trying to display my images in a HTML table but for some reason they just won't show up.
The images are located in a separate folder called 'images' and the image name is stored in my database as a varchar named e.g., filename.jpg
In my overview page I use this as my code:
<!DOCTYPE html>
<html lang="en">
<body>
<table>
<?php
foreach ($objectname as $key) {
echo '<tr><td><img src="images/' . $key->getImage() . '"></td></tr>';
}
?>
</table>
</body>
</html>
Hoping someone here can point me in the right direction :) thanks!!
You should first print print_r ($objectname), check if it comes with data and what its attributes are to see which one you need to occupy.
Second the $ key->getImage() getImage () = function () you are calling a function.
I think you should change the getImage() function in your html to the name of the field in the database as follows:
$key->getImage() to $key->Image_field_name_in_the_database
Because the name of the image (e.g: filename.jpg) is saved in a field of a table in your database.
Image_field_name_in_the_database will be the name of the field that contains the name of the image in the database.

Large data tables on MPDF - Can i avoid resize?

I'm generating some reports on my admin and I'm exporting to PDF using the MPDF library. But when this has a very large amount of data, MPDF is resizing the table to fit on page, so that it becomes awkward.
Is there any way to prevent this from happening? The code I am using to generate is the below. I've tried to set "shrink_tables_to_fit" to 0 and 1, both unsuccessful. Also tried to set autosize=1 to every table.
$stylesheet = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/site/css/layout/pdf.css');
$bootstrap = file_get_contents('https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css');
$mpdf = new mPDF('pt', 'a4');
$mpdf->shrink_tables_to_fit = 1;
$mpdf->setAutoTopMargin = 'stretch';
$mpdf->setAutoBottomMargin = 'stretch';
$mpdf->SetHTMLHeader('<img src="'.$_SERVER['DOCUMENT_ROOT'].'/site/img/content/pdf/header.jpg">');
$mpdf->SetFooter('{DATE j/m/Y H:i}|{PAGENO}/{nb}');
$mpdf->WriteHTML($bootstrap, 1);
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($html);
$mpdf->Output($_SERVER['DOCUMENT_ROOT'].'/upload/consultas-pre/'.$nomeArquivo.'.pdf', 'F');
You can split the entire table into different tables and at end of the each table add :
<div style="page-break-after: always;"> </div>
Which will create a page break.
Example :
<?php $rows=1; foreach($allData as $data){ ?>
<table>
<tr>
<td>Your table data</td>
</tr>
</table>
<?php if($row == 10) { ?>
<div style="page-break-after: always;"> </div>
<?php } ?>
<?php } ?>
You can read more about mpdf page break here
I found it.
I can not have a table with other tables inside. When this happens, MPDF gets lost in resize.
I had the same issue. Tried everything I found on stackoverflow in different combinations: shrink_tables_to_fit, autosize="0", page-break-inside:..., overflow:..., etc...
In my case my table was inside the div that had position:absolute in its inline style. After I changed it to relative or removed position completely, the table is divided properly between pages.
Try this in the table opening tag:
<table autosize="1">

how to put jpgraph into PDF using dompdf-0.5.1

It took me 12 hours still no improvement. I try displaying different images from my computer stored in the server and the result was successful. however when displaying the reports pie graph, it wont read every time the system tries to convert to pdf. It gives a blank pdf file. On the other hand, I can view the pie graph I created by using echo''; in the reports.php. I used the same concept in dompdf file but its not working.
DOMPDF
<html>
<head>
<title></title>
</head>
<body>
<?php echo'<img src="reports-display.php"/>';?>
</body>
</html>
<?php
$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
JPGRAPH Drawing
<?php
require('dbcon.php');
require_once('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_pie.php');
require_once ('jpgraph/src/jpgraph_pie3d.php');
//LEGEND
//YELLOW=LIVE BLUE=WAITING GREEN=DONE
//sql query for live
$live = mysql_query("Select count(*) as count1 from tbl_display_ads where status LIKE '%Live%'") or die(mysql_error());
//sql query for waiting
$waiting = mysql_query("Select count(*) as count2 from tbl_display_ads where status LIKE '%Waiting%'") or die(mysql_error());
//sql query for done/posted advertisement
$done = mysql_query("Select count(*) as count3 from tbl_display_ads where status LIKE '%Done%'") or die(mysql_error());
//While loop for live
while($resultlive = mysql_fetch_array($live))
{
$totallive = $resultlive['count1'];
}
//While loop for waiting
while($resultwaiting = mysql_fetch_array($waiting))
{
$totalwaiting = $resultwaiting['count2'];
}
//While loop for done
while($resultdone = mysql_fetch_array($done))
{
$totaldone = $resultdone['count3'];
}
// Some data
$data = array($totallive,$totalwaiting,$totaldone);
// Create the Pie Graph.
$graph = new PieGraph(500,450);
$theme_class= new VividTheme;
$graph->SetTheme($theme_class);
// Set A title for the plot
$graph->title->Set("Figure 1.1: Totality of Display Advertisement");
// Create
$p1 = new PiePlot3D($data);
$p1->SetCenter(0.5,0.55);
$p1->SetLegends(array("Live","Waiting","Done"));
$graph->legend->SetPos(0.5,0.100,'center','bottom');
$graph->Add($p1);
$p1->ShowBorder();
$p1->SetColor('black');
$p1->ExplodeSlice(1);
$graph->Stroke();
// Get the handler to prevent the library from sending the
// image to the browser
$gdImgHandler = $graph->Stroke(_IMG_HANDLER);
// Stroke image to a file and browser
// Default is PNG so use ".png" as suffix
$fileName = "/tmp/imagefile.png";
$graph->img->Stream($fileName);
// Send it back to browser
$graph->img->Headers();
$graph->img->Stream();
?>
I finally found out the solution. in the report-display.php I set the extension name of the graph to .png and save to the directory folder for reports.
DEFINE("DEFAULT_GFORMAT","auto");
$graph->img->SetImgFormat("png");
if(file_exists("Reports/reports-display.png")) unlink("Reports/reports-display.png");
$graph->Stroke("Reports/reports-display.png");
The problem is that you're essentially asking dompdf to grab an image file called "reports-display.php" from the local filesystem. When you use $dompdf->load_html() dompdf has no idea where the content arrives from. Any resource references in the HTML that lack a full URL are pulled in via the local filesystem. Since dompdf does not parse the PHP the source will be read in, which is obviously not a valid image document.
You're found a valid solution in saving the file locally. There are two other possibilities:
1) Point to the jpgraph script through your web server.
<html>
<head>
<title></title>
</head>
<body>
<img src="http://example.com/reports-display.php"/>
</body>
</html>
2) Capture the jpgraph output and insert into the document as a data-uri.
<html>
<head>
<title></title>
</head>
<body>
<img src="data:image/png;base64,<?php echo base64_encode(include_once('reports-display.php');?>"/>
</body>
</html>
With this method reports-deplay.php would have to be updated to return the image rather than stream it. Something like:
$graph = new PieGraph(500,450);
// snip steps that generate the content
return $graph->Stroke();

HTML to PDF when TCPDF base on foreach and while

This might have been already answered, yes, I used Google, and the search here stackoverflow, but the problem remains...
I'm using TCPDF library to generate PDF file, it works just fine, but I have the following situation, in order for my to generate a PDF ot just the HTML I need to put a few foreach and while's and IF's inside the HTML so that I can get the layout that the user is requesting...
so...
if(isset($_POST['submit'])) {
$ids = htmlentities($_POST['id'], ENT_NOQUOTE;
$con = conectionDB();
$query 'SELECT * FROM books WHERE id = "$ids"';
$doit = $con->query($query);
// at this point everything is file
// a few if's and we are done with the fetching "books" data
// a few other tuff that is required from the library nothing fancy...
// now here is the hard part
// next line will build my layout to display my PDF
$build_HTML = <<<EOD
<style>
.clasess {...}
.nother {...}
</style>
<table width="100%" border="1" cellpadding="0" bordercolor="#333333" >
<tr>
<td>Author</td>
<td>Books</td>
</tr>
<tr>
<td> Jong </td>
<td>
<table>
<tr>
<td>Title</td><td>Year</td>
</tr>
// Here is the problem I need to put a query to fetch the related data from
// another table
$books = conectionDB();
$bookQ = "SELECT * FROM titles WHERE name = '$author_name'";
$doitTitles = $books->query($bookQ);
if ($doitTitles->num_row > 1) {
while($dos = $doitTitles->fetch_assoc()){
// my td's, but this doest work...
}
}
</table>
</td>
</tr>
</table>
EOD;
$pdf->writeHTML($build_HTML, true, false, false, false, '');
} else {
// Go back...
}
As you can see I need that query right there, you may have notice that I have $doitTitles->num_row > 1 why? because if there is more than 1 tittle the layout would different if there is only 1 record...
We know that, that wont work, so the question is, is there another way to do that?
now, before the user go to the PDF, I display the information in plain html, which mean that the HTML that I use before the user go in to the PDF will be the same... so I was wondering, there another library that I can use to render the HTML in to PDF instead of building the PDF from inside the file... the user is able to see the result so those result just put 'em in a PDF...
Thank you!
I have the solution, so the thing is that I'm trying to output an html that is been build with dynamic content, if that was to be show as any other page, my_page.html there is no problem, but in PDF using <<
$raw_html = 'Tables';
// a few while and foreach's and queries
$raw_html .='Closed tables';
// Ready the HTML
$html = <<<EOD
$raw_html
EOD;
$pdf->writeHTML($html, true, false, false, false, '');
$pdf->Output('pdf_file_with_dynamic_html.pdf', 'I');
// the end.
by preparing the html before the output it gives me the data I need the way i need it...
that solved my problem.

Categories