-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
343 lines (294 loc) · 8.82 KB
/
Copy pathindex.php
File metadata and controls
343 lines (294 loc) · 8.82 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDF Cloud Printer — Expedy</title>
<style>
/* ---------------------------------------------------------------
PDF to Cloud Printer — Pure CSS styling
Powered by Expedy — https://www.expedy.io
--------------------------------------------------------------- */
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: #f5f7fa;
color: #333;
line-height: 1.6;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem 1rem;
}
.container {
width: 100%;
max-width: 520px;
background: #fff;
border-radius: 12px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
padding: 2rem;
}
h1 {
font-size: 1.5rem;
font-weight: 700;
margin-bottom: 0.25rem;
color: #1a1a2e;
}
.subtitle {
font-size: 0.875rem;
color: #888;
margin-bottom: 1.75rem;
}
.subtitle a {
color: #4a6cf7;
text-decoration: none;
}
.subtitle a:hover {
text-decoration: underline;
}
label {
display: block;
font-size: 0.875rem;
font-weight: 600;
color: #444;
margin-bottom: 0.4rem;
}
input[type="text"] {
width: 100%;
padding: 0.6rem 0.75rem;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 0.95rem;
transition: border-color 0.2s;
outline: none;
}
input[type="text"]:focus {
border-color: #4a6cf7;
box-shadow: 0 0 0 3px rgba(74, 108, 247, 0.12);
}
.separator {
display: flex;
align-items: center;
margin: 1.25rem 0;
color: #aaa;
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 0.05em;
}
.separator::before,
.separator::after {
content: "";
flex: 1;
height: 1px;
background: #e0e0e0;
}
.separator::before { margin-right: 0.75rem; }
.separator::after { margin-left: 0.75rem; }
.file-upload {
position: relative;
border: 2px dashed #d0d5dd;
border-radius: 8px;
padding: 1.25rem;
text-align: center;
cursor: pointer;
transition: border-color 0.2s, background 0.2s;
}
.file-upload:hover {
border-color: #4a6cf7;
background: #f8f9ff;
}
.file-upload input[type="file"] {
position: absolute;
inset: 0;
opacity: 0;
cursor: pointer;
}
.file-upload-label {
font-size: 0.9rem;
color: #666;
}
.file-upload-label strong {
color: #4a6cf7;
}
button[type="submit"] {
width: 100%;
margin-top: 1.5rem;
padding: 0.7rem;
background: #4a6cf7;
color: #fff;
border: none;
border-radius: 8px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
button[type="submit"]:hover {
background: #3a5ce5;
}
button[type="submit"]:active {
background: #2f4ed0;
}
.message {
margin-top: 1.25rem;
padding: 0.75rem 1rem;
border-radius: 8px;
font-size: 0.9rem;
}
.message.success {
background: #eafbea;
color: #1a7a1a;
border: 1px solid #b6e6b6;
}
.message.error {
background: #fdeaea;
color: #a11;
border: 1px solid #f0bfbf;
}
footer {
margin-top: 2rem;
font-size: 0.8rem;
color: #aaa;
text-align: center;
}
footer a {
color: #4a6cf7;
text-decoration: none;
}
footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<!--
PDF to Cloud Printer — Web interface
Sends text or uploaded PDF files to a remote printer via the Expedy Cloud Print API.
More info: https://www.expedy.io
Tutorial: https://docs.expedy.io/en/usb-cloud-printer-adapter/tutorials/print-a-pdf-from-your-computer-to-a-cloud-printer
Raspberry Pi setup: https://docs.expedy.io/en/usb-cloud-printer-adapter/rpi-installation/installation-on-raspberry-pi
-->
<div class="container">
<h1>PDF Cloud Printer</h1>
<p class="subtitle">Send to your printer via <a href="https://www.expedy.io" target="_blank">Expedy</a></p>
<form method="post" action="" enctype="multipart/form-data">
<label for="text">Text to print</label>
<input type="text" id="text" name="text" placeholder="Type your text here...">
<div class="separator">or</div>
<label>Upload PDF files</label>
<div class="file-upload">
<input type="file" id="files" name="files[]" multiple accept=".pdf">
<div class="file-upload-label"><strong>Choose files</strong> or drag & drop PDF files here</div>
</div>
<button type="submit" name="submit">Print</button>
</form>
<?php
// ---------------------------------------------------------------
// PDF to Cloud Printer
// Powered by Expedy — https://www.expedy.io
// Documentation: https://docs.expedy.io
// ---------------------------------------------------------------
// Handle form submission
if (isset($_POST['submit'])) {
// Check that the user provided text or at least one file
if (!empty($_POST['text']) || !empty($_FILES['files']['tmp_name'][0])) {
// Retrieve the text entered by the user
$text = $_POST['text'];
// Generate a unique PDF filename using the current date and time
$pdfName = "pdf_" . date('Ymd_His') . ".pdf";
// Include the FPDF library (PDF generation) and FPDI (PDF import/merge)
// FPDF: http://www.fpdf.org/
// FPDI: https://www.setasign.com/products/fpdi/about/
require('fpdf/fpdf.php');
require('fpdi/src/autoload.php');
// Create an FPDI instance (extends FPDF) with A6 page size (105 x 148 mm)
$pdf = new \setasign\Fpdi\Fpdi('P', 'mm', array(105, 148));
// Process uploaded PDF files
if (!empty($_FILES['files']['tmp_name'][0])) {
foreach ($_FILES['files']['tmp_name'] as $file) {
if (!empty($file) && file_exists($file)) {
// Import all pages from the uploaded PDF into the output document
$pageCount = $pdf->setSourceFile($file);
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
$tplIdx = $pdf->importPage($pageNumber);
$pdf->AddPage();
$pdf->useTemplate($tplIdx);
}
}
}
}
// If the user entered text, add a page with that text
if (!empty($text)) {
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40, 10, $text);
}
// Save the generated PDF to disk
$pdf->Output('F', $pdfName);
// Build the public URL for the generated PDF and send it to the Expedy Cloud Print API
$file_url = 'https://' . $_SERVER['HTTP_HOST'] . '/' . $pdfName;
sendToExpedyPrinter($file_url);
echo '<div class="message success">Your files have been sent to the printer!</div>';
} else {
echo '<div class="message error">No text or PDF file provided.</div>';
}
}
/**
* Send a PDF to a remote printer via the Expedy Cloud Print API.
*
* API documentation: https://www.expedy.io
* Raspberry Pi setup: https://docs.expedy.io/en/usb-cloud-printer-adapter/rpi-installation/installation-on-raspberry-pi
*
* @param string $file_url Public URL of the PDF file to print
*/
function sendToExpedyPrinter($file_url) {
// Your printer UID from the Expedy dashboard — https://www.expedy.io
$printer_uid = 'ENTER YOUR PRINTER UID';
// Payload: the PDF URL to print and an origin tag for tracking
$data = array(
'usb_msg' => $file_url,
'origin' => 'Your defined origin tag.. a uri, a name ..'
);
// Configure the cURL request to the Expedy Cloud Print API
$options = array(
CURLOPT_URL => "https://www.expedy.fr/api/v2/devices/" . $printer_uid . "/usb/4/print",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => array(
"Accept: application/json",
// Your API credentials — format: token:secretkey
"Authorization: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Content-Type: application/json"
),
);
// Execute the API request
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo '<div class="message error">cURL Error: ' . htmlspecialchars($err) . '</div>';
} else {
echo $response;
}
}
?>
</div>
<footer>
Powered by <a href="https://www.expedy.io" target="_blank">Expedy</a> ·
<a href="https://docs.expedy.io/en/usb-cloud-printer-adapter/tutorials/print-a-pdf-from-your-computer-to-a-cloud-printer" target="_blank">Tutorial</a> ·
<a href="https://docs.expedy.io/en/usb-cloud-printer-adapter/rpi-installation/installation-on-raspberry-pi" target="_blank">Raspberry Pi setup</a>
</footer>
</body>
</html>