PdfController.php 13.5 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($pdfId)
    {
      
        $rows = new pdfModel();
        $rows = $rows->getpdf($pdfId)->get();

        $folder = $rows[0]->folder;
        $searchpath = "$folder";
        $pdffile =   $searchpath.'/'.$rows[0]->file;
        //$outfile="";

		$pageHeight = $rows[0]->pheight;
		$pageWidth = $rows[0]->pwidth;
		
        $storagePath  = Storage::disk('public')->getDriver()->getAdapter()->getPathPrefix();
        $outfile =  $storagePath.$searchpath."/new_".$rows[0]->file;
        $download = '/'.$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", "Tenzing Communications");
            $p->set_info("Title", $rows[0]->name );

            /* 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($pageWidth, $pageHeight , "");

		
                /* 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 == 'textflow') {

                            //Get font var from row

                            $textline = $row->content;
                            $textline = str_replace('<p>','<nextparagraph leading=100%>', $textline);
                            $textline = str_replace('</p>','', $textline);
                            $textline = str_replace('<br />','<nextline leading=80%>', $textline);
                            $textline = str_replace('<strong>','<fakebold=true>', $textline);
                            $textline = str_replace('</strong>','<fakebold=false>', $textline);
                            $textline = str_replace('<em>','<italicangle=-12>', $textline);
                            $textline = str_replace('</em>','<italicangle=0>', $textline);
                            $textline = str_replace('&amp;','&', $textline);
                            $textLUp = $row->locationUp;
							$textwidth = $row->width;
                            $textheight = $row->height;
                            $textLRight = $row->locationRight;
                            $fontStyles = unserialize($row->style);
							
							$fontAlignment = $fontStyles["alignment"];
							$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());
                            }
							$p->setfont($font, $fontSize);
							$num_optlist = "fontname=".$fontFamily." fontsize=".$fontSize." encoding=unicode alignment=".$fontAlignment." fillcolor={".$fontColor." } ";
			

							$textline = $p->create_textflow($textline , $num_optlist);
							$fit_optlist = "firstlinedist=capheight ";
                          
                            
                            $result = $p->fit_textflow($textline,  $textLRight, $textLUp, ($textwidth + $textLRight) ,($textheight +  $textLUp) ,$fit_optlist);

                        } else if ($row->change_type == 'text' OR $row->change_type == 'user') {
							$yoff = 50;
                            //Get font var from row

                            $textline = $row->content;
                            $textLUp = $row->locationUp;
							$textwidth = $row->width;
                            $textheight = $row->height;
                            $textLRight = $row->locationRight;
                            $fontStyles = unserialize($row->style);
							$fitmethod = $fontStyles["fitmethod"];
							
							$fontAlignment = $fontStyles["alignment"];
							$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());
                            }
							$p->setfont($font, $fontSize);
							//$num_optlist = "fontname=".$fontFamily." fontsize=".$fontSize." encoding=unicode alignment=".$fontAlignment." fillcolor={".$fontColor." }";
							if($fitmethod == ''){$fitm ="";
								}else{ $fitm = "fitmethod=".$fitmethod;}

							$num_optlist = "fillcolor={".$fontColor." }  position={".$fontAlignment."}   boxsize={" . $textwidth." ".$textheight . "}   fontsize=".$fontSize." ".$fitm. " ";
                       
                            $p->fit_textline($textline,  $textLRight, $textLUp, $num_optlist);
                        } else if ($row->change_type == 'date') {
                        $yoff = 50;
                        //Get font var from row

                        $textline = $row->content;
                        $textLUp = $row->locationUp;
                        $textwidth = $row->width;
                        $textheight = $row->height;
                        $textLRight = $row->locationRight;
                        $fontStyles = unserialize($row->style);
                        $fitmethod = $fontStyles["fitmethod"];

                        $fontAlignment = $fontStyles["alignment"];
                        $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());
                        }
                        $p->setfont($font, $fontSize);
                        //$num_optlist = "fontname=".$fontFamily." fontsize=".$fontSize." encoding=unicode alignment=".$fontAlignment." fillcolor={".$fontColor." }";
                        if($fitmethod == ''){$fitm ="";
                        }else{ $fitm = "fitmethod=".$fitmethod;}

                        $num_optlist = "fillcolor={".$fontColor." }  position={".$fontAlignment."}   boxsize={" . $textwidth." ".$textheight . "}   fontsize=".$fontSize." ".$fitm. " ";

                        $p->fit_textline($textline,  $textLRight, $textLUp, $num_optlist);
                    }

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

                            //Get image var from row

                            $imagefile = $row->folder . '/' . str_replace(' ', '-',  $row->content);
                            $imgLUp = $row->locationUp;
                            $imgLRight = $row->locationRight;
							$bw = $row->width;
                            $bh = $row->height;
							$ImageStyles = unserialize($row->style);
							//var_dump($ImageStyles);
							$ImagePosition = $ImageStyles["position"];
							$ImageFitmethod = $ImageStyles["fitmethod"];
							$ImageScale = $ImageStyles["scale"];
							$ImagesOrientate = $ImageStyles["orientate"];

                            //Load and insert the images
                            $buf = "boxsize={" . $bw . " " . $bh .  "} position={".$ImagePosition."} scale=".$ImageScale." orientate=".$ImagesOrientate." fitmethod=".$ImageFitmethod."  ";
                            $image = $p->load_image("auto", $storagePath . $imagefile,  "");
                            if ($image == 0) {
                                die("Error: " . $p->get_errmsg());
                            }
                            $p->fit_image($image,  $imgLRight, $imgLUp, $buf);


                        }else if ($row->change_type == 'texthidden') {
							$yoff = 50;
                            //Get font var from row

                            $textline = $row->content;
                            $textLUp = $row->locationUp;
							$textwidth = $row->width;
                            $textheight = $row->height;
                            $textLRight = $row->locationRight;
                            $fontStyles = unserialize($row->style);
							
							$fontAlignment = $fontStyles["alignment"];
							$fontFamily = $fontStyles["font-family"];
                            $fontSize = $fontStyles["font-size"];
							$fitmethod = $fontStyles["fitmethod"];
                            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());
                            }
							$p->setfont($font, $fontSize);
							//$num_optlist = "fontname=".$fontFamily." fontsize=".$fontSize." encoding=unicode alignment=".$fontAlignment." fillcolor={".$fontColor." }";
							if($fitmethod == ''){$fitm ="";
								}else{ $fitm = "fitmethod=".$fitmethod;}

							$num_optlist = "fillcolor={".$fontColor." }  position={".$fontAlignment."} fitmethod=".$fitmethod."  boxsize={" . $textwidth." ".$textheight . "}   fontsize=".$fontSize."";
                            $p->setfont($font, $fontSize);
                            $p->fit_textline($textline,  $textLRight, $textLUp, $num_optlist);
                        } 
						
                    }


                }

                $p->close_pdi_page($page);

                $p->end_page_ext("");
            }

    $p->end_document("");


    return  $download;

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



    }


}