dtddjq9637 2015-07-03 05:14
浏览 50
已采纳

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

I am new in PHP. i am using TCPDF to generate pdf file with php coding. I have a problem. I am not able to send file inline to the browser. when i click on file link it begin to start. I want to send it to the browser inline.

Here is my Code

<?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'); 
?>
  • 写回答

1条回答 默认 最新

  • duanli4146 2015-07-03 05:22
    关注

    Although it works with I, it works for O as well.

    Try

    $pdf->Output('name.pdf', 'O');
    

    If the above does not work, it is just better to use the php header() function.

    header("Content-type: application/pdf");
    

    After using the header() function, just echo the content of the PDF file you created.

    This is what I found out in the documentation.

    • I: send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
    • D: send to the browser and force a file download with the name given by name.
    • F: save to a local server file with the name given by name.
    • S: return the document as a string (name is ignored).
    • FI: equivalent to F + I option
    • FD: equivalent to F + D option
    • E: return the document as base64 mime multi-part email attachment (RFC 2045)
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图