PdfController.php
5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?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;
}
}