PdfController.php 5.51 KB
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers;
use  App\pdfModel;
use URL;
use Storage;
use Response;
use File;
Use PDF;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use League\Flysystem\Filesystem;
use  PDFlib;




class PdfController extends Controller
{

    public function pdf($id)
    {

        $rows = new pdfModel();
        $rows = $rows->getpdf($id)->get();
        $folder = $rows[0]->folder;
        $searchpath = "$folder";
        $pdffile =   $searchpath.'/'.$rows[0]->file;
        $outfile="";
        $title = "Test Pages";
        $storagePath  = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix();
     //   $outfile =  $storagePath.$searchpath."/new_".$rows[0]->file;

try {

            $p = new PDFlib();


            $p->set_option("SearchPath={{" .  $searchpath . "}}");

            # This means we must check return values of load_font() etc.
            $p->set_option("errorpolicy=return");

            /* Enable the following line if you experience crashes on OS X
             * (see PDFlib-in-PHP-HowTo.pdf for details):
             * $p->set_option("usehostfonts=false");
             */

            /* all strings are expected as utf8 */
            $p->set_option("stringformat=utf8");
            if ($p->begin_document($outfile, "") == 0)
                die("Error: " .  $p->get_errmsg());

            $p->set_info("Creator", "Rock Star Jeff");
            $p->set_info("Title", $title );

            /* Open the input PDF */
            $response = $storagePath.$pdffile;

            $indoc = $p->open_pdi_document( $response, "");
            if ($indoc == 0)
                die("Error: " .  $p->get_errmsg());


            $endpage = (int) $p->pcos_get_number($indoc, "length:pages");

            /* Loop over all pages of the input document */
            $pg=0;
            for ($pageno = 1; $pageno <= $endpage; $pageno++) {
                $page = $p->open_pdi_page($indoc, $pageno, "");


                if ($page == 0)
                    die("Error: " . $p->get_errmsg());

                /* Dummy page size; will be adjusted later */
                $p->begin_page_ext(10, 10, "");

                /* Place the imported page on the output page, and
                 * adjust the page size   */

                $p->fit_pdi_page($page, 0, 0, "adjustpage");


                foreach($rows AS $row ) {
                    $pageNumber = $row->pages;
                    if($pageNumber == $pageno ) {

                        if ($row->change_type == 'text') {

                            //Get font var from row

                            $textline = $row->content;
                            $textLUp = $row->locationUp;
                            $textLRight = $row->locationRight;
                            $fontStyles = unserialize($row->style);

                            $fontFamily = $fontStyles["font-family"];
                            $fontSize = $fontStyles["font-size"];
                            if(isset($fontStyles["font-color"])) {
                                $fontColor = $fontStyles["font-color"];
                            }else{
                                $fontColor = 'cmyk 0.75 0.68 0.67 0.90';

                            }
                            /// Load and insert text and font

                            $p->set_option("FontOutline={" . $fontFamily . "=" . $storagePath . $folder . "/" . $fontFamily . ".otf}");
                            /* For PDFlib Lite: change "unicode" to "winansi" */
                            $font = $p->load_font($fontFamily, "unicode", "embedding");
                            if ($font == 0) {
                                die("Error: " . $p->get_errmsg());
                            }
                            $num_optlist = "fillcolor={".$fontColor." }";
                            $p->setfont($font, $fontSize);
                            $p->fit_textline($textline, $textLUp, $textLRight, $num_optlist);


                        } else if ($row->change_type == 'img') {

                            //Get image var from row

                            $imagefile = $row->folder . '/' . $row->content;
                            $imgLUp = $row->locationUp;
                            $imgLRight = $row->locationRight;

                            //Load and insert the images

                            $image = $p->load_image("auto", $storagePath . $imagefile, "");
                            if ($image == 0) {
                                die("Error: " . $p->get_errmsg());
                            }
                            $p->fit_image($image, $imgLUp, $imgLRight, "");


                        }
                    }


                }

                $p->close_pdi_page($page);

                $p->end_page_ext("");
            }

    $p->end_document("");


    /// To turn on pdf screen out put uncomment below lines and remove the text in $output="" Var

    $buf = $p->get_buffer();
    $len = strlen($buf);

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







}

        catch (PDFlibException $e) {
            die("PDFlib exception occurred:\n" .
                "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
                $e->get_errmsg() . "\n");
        }
        catch (Exception $e) {
            die($e);
        }

        $p = 0;



    }


}