如何使用php和TCPDF将文件内联发送到浏览器

如何使用php和TCPDF将文件内联发送到浏览器,php,mysql,pdf,tcpdf,Php,Mysql,Pdf,Tcpdf,我是PHP新手。我使用TCPDF生成带有php编码的pdf文件。我有个问题。我无法将文件内联发送到浏览器。当我点击文件链接时,它开始启动。我想将其发送到浏览器内联 这是我的密码 <?php require_once('tcpdf/tcpdf.php'); class MYPDF extends TCPDF { //Page header public function Header() { // Logo $image_file ='imag

我是PHP新手。我使用TCPDF生成带有php编码的pdf文件。我有个问题。我无法将文件内联发送到浏览器。当我点击文件链接时,它开始启动。我想将其发送到浏览器内联

这是我的密码

<?php
require_once('tcpdf/tcpdf.php');
class MYPDF extends TCPDF {
    //Page header
    public function Header() {
        // Logo
        $image_file ='image/pacra.jpg';
        $this->Image($image_file, 100, 05, 15);
        // Set font
        $this->SetFont('helvetica', 'I', 15);

        // Title
        $this->SetTextColor(0,63,127);

      $this->Cell(0,50, 'The Pakistan Credit Rating Agency Limited', 0, false, 'C', 0, '', 0, false);
     // $this->setColor(0,63,127);
        $this->Line(10,30,200,30);
        /*$style = array('width' => 0.5, 'cap' => 'butt', 'join' => 'miter', 'dash' => '10,20,5,10', 'phase' => 10, 'color' => array(255, 0, 0));
        $this->Line(5, 10, 80, 30, $style);*/
    }
    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-25);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 05, 'Awami Complex FB-1, Usman Block, New Garden Town, Lahore - 54600, Pakistan'
, 0, false, 'C', 0, '', 0, false, 'T', 'M');
$this->Ln();
$this->Cell(0, 05, 'PABX: 92(42)3586 9504 Fax: 92(42)3583 0425 E-mail: pacra@pacra.com'
, 0, false, 'C', 0, '', 0, false, 'T', 'M');

    }
}
// create new PDF document
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->AddPage();

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="test123"; // Database name 
$tbl_name="form"; // Table name
$con = mysqli_connect('localhost','root','');
mysqli_select_db($con,"test123");
$sql="SELECT * FROM form WHERE Id=34";
$result = mysqli_query($con,$sql);
while($rows= (mysqli_fetch_array($result,MYSQLI_ASSOC)))
        {
            $name = $rows['Name'];
            $address = $rows['Address'];
            $class = $rows['Designation'];
            $phone = $rows['Text'];

            $pdf->SetXY(10,32);
        $pdf->SetFontSize(12);
        $pdf->SetTextColor(0,63,127);
        $pdf->writeHTML($name, true, false, true, false, '');
       $pdf->SetTextColor(0,0,0);
    $pdf->SetXY(180,32);
            $pdf->writeHTML($address, true, false, true, false, '');
            $pdf->SetXY(10,36);
                   $pdf->SetTextColor(0,0,0);
        $pdf->writeHTML($class, true, false, true, false, '');
        $pdf->SetXY(10,45);
        $pdf->writeHTML($phone, true, false, true, false, '');
        }
        $html= '<h6>This is test paragraph</h6>
        <br>
        <h6>This is another test paragraph</h6>
        ';
        $pdf->writeHTML($html, true, false, true, false, '');

$pdf->Output('test.pdf','I'); 
?>

虽然它与
I
一起工作,但它也适用于
O

试一试

如果上述方法不起作用,最好使用
php header()
函数

header("Content-type: application/pdf");
使用
header()
函数后,只需
echo
您创建的
PDF
文件的内容即可

这是我在文档中发现的。

  • I:将文件内联发送到浏览器(默认)。如果可用,则使用该插件。选择时,将使用名称给出的名称 生成PDF的链接上的“另存为”选项
  • D:发送到浏览器并强制下载名为的文件
  • F:使用名称指定的名称保存到本地服务器文件
  • S:以字符串形式返回文档(忽略名称)
  • FI:相当于F+I期权
  • FD:相当于F+D选项
  • E:将文档作为base64 mime多部分电子邮件附件(RFC 2045)返回

当我使用header()函数并对生成的内容进行回显时,它对我起了作用。你试过了吗?在你看到文档中的“i”后,我将标题函数放在了“如果可用,请使用插件”中。我的案例中是否存在插件问题?你使用的是什么版本的TCPDF?也可能是浏览器问题,你使用的是哪种浏览器?
header("Content-type: application/pdf");