1

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 <> stream x�3R��2�35W(�r Q�w3T04�30PISp �Z*�[����(hx����+����(j*�d��7W)

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;
    }

Here is the ajax for pagination:

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);

    })

5 Answers 5

4

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:

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();

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! sounds great! does it mean in the show_pdf.php file, what i only need to add $_GET to check if the id value is asdfasdf and run the following code
2

To show fpdf in borwser pass second parameter 'I'

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

Comments

1

It looks like you're trying to display the PDF inline. To do this you should create an IFRAME with a source that points to your PDF creation script. That way the browser can choose whether or not it can display the PDF, if it can't then the file will be downloadable.

Main point: the browser doesn't know how to interpret what you're giving it.

2 Comments

thanks for your reply, could you tell me where should i add the iframe? in the else if ($cur_page == $no_of_paginations) block?
@user3112938 Your AJAX request is going to load_data.php, so that's what you want to load into the IFRAME. And you want to place the IFRAME in the HTML where you normally place the AJAX response (inside #container). It looks like you'll either have to change your PHP script to read $_GET variables rather than $_POST or you can use JS to create the IFRAME, then create a form with method="post" that targets your IFRAME and then submit the form.
0

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

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();

Comments

-1

Use a object, $('#fact').html('

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

');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.