简体   繁体   English

尝试使用PHP和fpdf显示pdf文件

[英]Try to show pdf file using PHP and fpdf

Background: Its' ae learning website, when user in the learning stage, it will show content for reading at first 3 pages, and after that it will need users to do the test, when users overcome all the challenges, it will show the pdf certificate. 背景:它的'ae学习网站,当用户处于学习阶段时,它将在前3页显示阅读内容,之后需要用户进行测试,当用户克服所有挑战时,它将显示pdf证书。

Currently, the problem is: after reaching the last page, it always showing me some weird words: (below is part of all) 目前,问题是:在到达最后一页后,它总是向我显示一些奇怪的词:(以下是所有的一部分)

(%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x 3R 2 35W( r Q w3T04 30PISp Z* [ (hx + (j* d 7?W) (%PDF-1.3 3 0 obj <> endobj 4 0 obj <>streamx 3R 2 35W( rQ w3T04 30PISp Z* [ (hx + ) (j * D7?W)

Here is the part of the code, whose file name is load_data.php 以下是代码的一部分,其文件名为load_data.php

else if ($cur_page == $no_of_paginations){


        ob_end_clean();
        require("../fpdf.php");

        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(40,10,'Hello World!');
        if (preg_match("/MSIE/i", $_SERVER["HTTP_USER_AGENT"])){
            header("Content-type: application/PDF");
        } else {
            header("Content-type: application/PDF");
            header("Content-Type: application/pdf");
        }
        $pdf->Output();
        exit;
    }

Here is the ajax for pagination: 这是分页的ajax:

function loadData(page){

//        debugger;
        loading_show();
        $.ajax
        ({
            type: "POST",
            url: "load_data.php",
            data: { page: +page, tableName:  $.urlParam('modules') },
            //data: "page="+page,
            success: function(msg)
            {
                $("#container").ajaxComplete(function(event, request, settings)
                {
                    loading_hide();
                    $("#container").html(msg);
                });
            }
        });
    }
    loadData(1);  // For first time page load default results
    $('#container .pagination li.active').live('click',function(){
        var page = $(this).attr('p');
        loadData(page);

    })

FPDF allows you a few options in outputting PDFs. FPDF允许您输出PDF的几个选项。 You can force the download, save the file to a filesystem, or you can display them on the page, but what you can't do is generate a PDF and show it inline as Jasper mentioned. 您可以强制下载,将文件保存到文件系统,或者您可以在页面上显示它们,但您不能做的是生成PDF并将其显示为内嵌,如Jasper所述。 Here's what you'll do: 这是你要做的:

Create a new file (maybe, show_pdf.php): 创建一个新文件(可能是show_pdf.php):

    require("../fpdf.php");

    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    if (preg_match("/MSIE/i", $_SERVER["HTTP_USER_AGENT"])){
        header("Content-type: application/PDF");
    } else {
        header("Content-type: application/PDF");
        header("Content-Type: application/pdf");
    }
    $pdf->Output();

You may need to pass in specific data parameters via $_GET to make this work right as you expand this code. 您可能需要通过$ _GET传递特定的数据参数,以便在展开此代码时使其正常工作。

To show it on the page, you'll do something like this: 要在页面上显示它,您将执行以下操作:

else if ($cur_page == $no_of_paginations){
?>
<iframe src="show_pdf.php?id=asdfasdf"></iframe>
<?php
}

It looks like you're trying to display the PDF inline. 看起来您正在尝试以内联方式显示PDF。 To do this you should create an IFRAME with a source that points to your PDF creation script. 为此,您应该创建一个IFRAME,其中包含指向PDF创建脚本的源。 That way the browser can choose whether or not it can display the PDF, if it can't then the file will be downloadable. 这样浏览器可以选择是否可以显示PDF,如果不能,则可以下载该文件。

Main point: the browser doesn't know how to interpret what you're giving it. 要点:浏览器不知道如何解释你给它的东西。

在borwser中显示fpdf传递第二个参数'I'

 $filed = $pdf->Output('abc.pdf', 'I');

Yii::$app->response->format = \\yii\\web\\Response::FORMAT_RAW; Yii :: $ app-> response-> format = \\ yii \\ web \\ Response :: FORMAT_RAW;

Yii::$app->response->headers->add('Content-Type', 'application/pdf'); Yii :: $ app-> response-> headers-> add('Content-Type','application / pdf');

    $pdf = new FPDF();

    $pdf->AliasNbPages();
    $pdf->AddPage();
    $pdf->SetFont('Arial', 'B', 15);
    $pdf->Cell(40, 10, 'Hello World');
    $pdf->Output();

Use a object, $('#fact').html(' 使用对象$('#fact')。html('

Usted no tiene instalado un pugin para .pdf en este navegador. Usted no tiene instalado un pugin para .pdf en este navegador。
Usted puede usar Este link para descargar el archivo PDF. Usted puede usar Este link para descargar el archivo PDF。

'); “);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM