尝试使用PHP和fpdf显示pdf文件 [英] Try to show pdf file using PHP and fpdf

查看:161
本文介绍了尝试使用PHP和fpdf显示pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:这是一个学习型网站,当用户处于学习阶段时,它将在前三页显示要阅读的内容,之后,当用户访问时,需要用户进行测试克服所有挑战,它将显示pdf证书.

Background: Its' a e 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.

当前的问题是:到达最后一页后,它总是向我显示一些奇怪的词:(下面是全部的一部分)

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<>流x 3R 2 35W( rQ w3T04 30PISp Z* [ (hx. + (j * d 7?W)

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

这是代码的一部分,其文件名为load_data.php

Here is the part of the code, whose file name is 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;
    }

这是分页的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在输出PDF时为您提供了一些选择.您可以强制下载,将文件保存到文件系统,也可以在页面上显示它们,但是您不能做的就是生成PDF并按照Jasper的说明内联显示.这是您要执行的操作:

FPDF allows you a few options in outputting PDFs. 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. Here's what you'll do:

创建一个新文件(也许是show_pdf.php):

Create a new file (maybe, 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();

您可能需要通过$ _GET传递特定的数据参数,才能在扩展此代码时正常工作.

You may need to pass in specific data parameters via $_GET to make this work right as you expand this code.

要在页面上显示它,您将执行以下操作:

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
}

这篇关于尝试使用PHP和fpdf显示pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆