Displaying a PDF in <object> tag in CodeIgniter view

14,019

Try this way, create a new method to generate PDF instead of using same method you use to load view. Then use that method as the data-src for object tag.

For example:

Function to generate pdf and stream it to browser

function generate_pdf()
{
    $doc = new Docx_reader();
    $doc->setFile('testDoc3.docx');

    $plain_text = $doc->to_plain_text();
    $html = $doc->to_html();

    $pdf = pdf_create($html, 'testDoc4', false);
    $len = strlen($pdf);
    header("Content-type: application/pdf");
    header("Content-Length:" . $len);
    header("Content-Disposition: inline; filename=Resume.pdf");
    print $pdf;
} 

Function to load your view:

function load_view()
{
    $this->load->view('results/test_viewer');
}

the test_viewer file:

<object data="<?php echo  base_url('YOUR_CONTROLLER/generate_pdf' ?>" width="100%" height="500px" type="application/pdf"> PDF Plugin Not Available </object>
Share:
14,019
jldavis76
Author by

jldavis76

Software Engineer

Updated on June 04, 2022

Comments

  • jldavis76
    jldavis76 almost 2 years

    Ultimately, what I would like to do is take a .docx file, convert it to HTML, then convert that HTML into a PDF and embed that PDF in my view. I want to do this all without saving the PDF to a file, but instead, just immediately displaying it in my view.

    So far, I have been able to convert the .docx into HTML and then convert that into a pdf. However, I am now stuck on how to display that pdf string in my view. Currently, I am trying to load a view that has headers for a pdf into a variable, and then send that variable to my view to display in tags. It's not working though.

    Here is what I have so far...

    My controller:

    $doc = new Docx_reader();
    $doc->setFile('testDoc3.docx');
    
    $plain_text = $doc->to_plain_text();
    $html = $doc->to_html();
    
    $pdf = pdf_create($html, 'testDoc4', false);
    $data['pdfx'] = $pdf;
    $data['pdf'] = $this->load->view('test_pdf_view', $data, TRUE);
    $this->load->view('results/test_viewer', $data);
    

    The test_pdf_view:

    $len = strlen($pdfx);
    header("Content-type: application/pdf");
    header("Content-Length:" . $len);
    header("Content-Disposition: inline; filename=Resume.pdf");
    print $pdfx;
    

    And the relevant part of my test_viewer view:

    <object data="<?php echo $pdf; ?>" width="600" height="775" type="application/pdf"> PDF Plugin Not Available </object>
    

    the pdf_create function:

    function pdf_create($html, $filename='', $stream=TRUE)
    {
        require_once("system/helpers/dompdf/dompdf_config.inc.php");
    
        $dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        if ($stream) {
            $dompdf->stream($filename.".pdf");
        } else {
            return $dompdf->output();
        }
    }
    

    Is what I'm trying to do even possible? I know that I could save the pdf to a file or database, and then display that pdf file, and ultimately delete or unlink that file, but for a few reasons that is not the direction I would like to go. Any ideas or help is appreciated!

    Thanks!

    • whitwhoa
      whitwhoa over 8 years
      What is pdf_create() returning? Also when you are loading the view...where is $text coming from?
    • jldavis76
      jldavis76 over 8 years
      It returns a pdf string. I've edited my question to show that function. Also, $text is a type that I forgot to change back to $data. The question has been edited to fix that as well.
    • whitwhoa
      whitwhoa over 8 years
      I think the best thing to do would be to save the files on the filesystem with a timestamp in the name and have a background task that runs every so often to remove the old files. BUT...there are some answers in this question that might work for you: stackoverflow.com/questions/12876000/…
  • jldavis76
    jldavis76 over 8 years
    This worked very well actually, but it was extremely slow. So I think I'm going to need to come up with a different solution. Any experience using LibreOffice through the command line to do the conversion.
  • Syam
    Syam over 8 years
    I haven't tried using LibreOffice, but this (stackoverflow.com/a/14136403/4468838) answer explains doc to pdf conversion using open office. This might help you.