我正在使用dompdf库并生成PDF文件。在该PDF中,我必须将来自查询的内容放入数据库中。如何使用SQL查询获取内容来生成PDF文件?
0
A
回答
0
请试试这个脚本
<?php
$mysqli = new mysqli("localhost", "root", "", "");
if ($mysqli->connect_errno) {
echo "Failed to Connect MySQL: (" . $mysqli- >connect_errno . ") " . $mysqli->connect_error;
}
require_once 'dompdf-master/dompdf_config.inc.php';
$result=$mysqli->query("SELECT * FROM tbl_name");
while ($arr_result = $result->fetch_array())
{
$nombre=$arr_result["nombre"];
$html.= "
<table>
<tr>
<td> $nombre </td>
</tr></table>
";
$mipdf = new DOMPDF();
$mipdf ->set_paper("A4", "portrait");
$mipdf ->load_html(utf8_decode($html));
$mipdf ->render();
$mipdf ->stream('mipdf.pdf');
}
?>
0
您可以使用FPDF。 FPDF是一个PHP类,允许使用纯PHP生成PDF文件,也就是说不使用PDFlib库。 FPDF的F表示免费:您可以将其用于任何形式的使用,并对其进行修改以适应您的需求。
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
上面的代码会生成一个PDF格式的“Hello World!”。文字,字体,大小16和粗体。 您可以输入您的sql查询结果来代替Hello World。
有关FPDF的更多信息,请转到official site。
0
FPDF
FPDF是一个PHP类,允许你创建一个基于PHP的PDF文档。 可以使用include
提供数据库连接。此外,可以使用FPDF类的标准化函数给出列标题,pdf的方向。
<?php
define('FPDF_FONTPATH', 'font/');
require('fpdf.php');
//Connect to your database
include("conectmysql.php");
//Create new pdf file
$pdf=new FPDF();
//Open file
$pdf->Open();
//Disable automatic page break
$pdf->SetAutoPageBreak(false);
//Add first page
$pdf->AddPage();
//set initial y axis position per page
$y_axis_initial = 25;
//print column titles for the actual page
$pdf->SetFillColor(232, 232, 232);
$pdf->SetFont('Arial', 'B', 12);
$pdf->SetY($y_axis_initial);
$pdf->SetX(25);
$pdf->Cell(30, 6, 'CODE', 1, 0, 'L', 1);
$pdf->Cell(100, 6, 'NAME', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'PRICE', 1, 0, 'R', 1);
$y_axis = $y_axis + $row_height;
//Select the Products you want to show in your PDF file
$result=mysql_query('select Code, Name, Price from Products ORDER BY Code', $link);
//initialize counter
$i = 0;
//Set maximum rows per page
$max = 25;
//Set Row Height
$row_height = 6;
while($row = mysql_fetch_array($result))
{
//If the current row is the last one, create new page and print column title
if ($i == $max)
{
$pdf->AddPage();
//print column titles for the current page
$pdf->SetY($y_axis_initial);
$pdf->SetX(25);
$pdf->Cell(30, 6, 'CODE', 1, 0, 'L', 1);
$pdf->Cell(100, 6, 'NAME', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'PRICE', 1, 0, 'R', 1);
//Go to next row
$y_axis = $y_axis + $row_height;
//Set $i variable to 0 (first row)
$i = 0;
}
$code = $row['Code'];
$price = $row['Price'];
$name = $row['Code'];
$pdf->SetY($y_axis);
$pdf->SetX(25);
$pdf->Cell(30, 6, $code, 1, 0, 'L', 1);
$pdf->Cell(100, 6, $name, 1, 0, 'L', 1);
$pdf->Cell(30, 6, $price, 1, 0, 'R', 1);
//Go to next row
$y_axis = $y_axis + $row_height;
$i = $i + 1;
}
mysql_close($link);
//Create file
$pdf->Output();
?>
相关问题
- 1. 我要生成SQL查询的内容
- 2. 如何使用iText库获取pdf文件的段落内容?
- 3. 如何获取Web View的全部内容以在Android中生成PDF文件?
- 4. 如何在iOS上使用“真实”文本内容生成PDF?
- 5. 获取SQL查询的TableCell内容
- 6. SQL查询不获取所有内容
- 7. 获取Itext中的pdf文件内容
- 8. Phonegap从PDF文件中获取内容
- 9. 如何生成SQL查询
- 10. 如何使用PhP获取外部ASP文件的动态生成内容?
- 11. 生成SQL查询来获取有关结果
- 12. PDF生成动态内容
- 13. PHP获取动态生成文件的文件内容
- 14. 如何编写SQL查询以获取以下内容...
- 15. 如何使用子查询来查询学生成绩?
- 16. 从SharePoint内容数据库,使用SQL服务器查询如何获取'文档GUID'基于'内容类型'
- 17. SQL查询来获取最
- 18. SQL查询来获取
- 19. SQL查询来获取
- 20. 如何获取JPA生成的SQL查询?
- 21. 如何在使用C#生成后生成pdf文件大小
- 22. 如何使用wkhtmltopdf为pdf生成设置内容字体?
- 23. 从sql查询生成.orm.yml文件
- 24. 无法使用TCPDF生成来自MySQL查询的PDF
- 25. 如何使用GWT查询获取iframe的内容?
- 26. 如何使用getTranslatedSQLString获取Toplink生成的查询?
- 27. 如何使用字符串生成器来读取word文档内容?
- 28. AEM:如何使用查询生成器来获取节点名称'XX'?
- 29. 获取使用SQL查询
- 30. 使用jquery获取gdocsviewer插件生成的内容
嗨Saravanan。就你所知,像这样的简短问题往往会被降低和/或关闭。这是因为它的简洁隐藏了我们需要提供的可能对你有用的答案的细节(尽管如此,无论如何,两个人都勇敢地给出答案)。另外,我们喜欢在这里看到先前的努力和研究,而这两个特征都不被看作是懒惰的(在这种情况下,你可能已经做了几天的研究,但是因为我们看不到它,所以我们假设没有)。 – halfer 2015-03-03 09:05:11
嗨。我运行查询并从数据库获取字段名称和值。但它需要字段名称而不考虑字段值。 mysql_num_fields()这个方法用来获取字段名和mysql_fetch_row()这个方法用来获取字段值。 – 2015-03-03 10:00:41
这将是最有帮助的,在这种情况下,如果你将你的代码的相关位放入你的问题(通过编辑它)。希望你现在可以明白为什么这是至关重要的。请使用提供的格式工具。 – halfer 2015-03-03 10:01:44