-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertSomeHTML.php
More file actions
82 lines (74 loc) · 2.13 KB
/
convertSomeHTML.php
File metadata and controls
82 lines (74 loc) · 2.13 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
<?php
define('__ROOT__', dirname(__FILE__));
require_once(__ROOT__.'/includes/dompdf/autoload.inc.php');
require_once(__ROOT__.'/includes/tcpdf/tcpdf.php');
require_once(__ROOT__.'/includes/tcpdf/tcpdi.php');
require_once(__ROOT__.'/includes/fpdi/src/autoload.php');
use Dompdf\Dompdf;
function doConvert() {
$input = resolveInput();
if (isset($input['authKey'])) {
if (isAuthenticated($input['authKey'])) {
if (isset($input['html'])) {
convertHTMLAndOutput($input['html']);
}
else if (isset($input['pages']) && is_array($input['pages'])) {
handleInputPages($input['pages']);
}
}
else {
header("HTTP/1.1 403 Unauthorized");
}
}
}
function resolveInput() {
$requestBody = file_get_contents('php://input');
if (empty($requestBody)) {
error_log("No Input");
return;
}
$inputArray = json_decode($requestBody, true);
return $inputArray;
}
function isAuthenticated($key) {
$authKeys = array_diff(scandir(__ROOT__.'/apiKeys'), array('..', '.'));
if (in_array($key, $authKeys)) {
return true;
}
return false;
}
function handleInputPages($pageArray) {
$pdfArray = [];
foreach ($pageArray as $page) {
$pdfArray[] = domPDFConversion($page);
}
$combinedPDF = mergePages($pdfArray);
echo $combinedPDF;
}
function domPDFConversion($html) {
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
return $dompdf->output();
}
function mergePages($pageArray) {
$pdf = new TCPDI('P', 'mm', 'A4', true, 'UTF-8', false, false);
$pdf->SetTitle('Output.pdf');
foreach ($pageArray as $page) {
$pdf->setSourceData($page);
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 0, 0, true);
}
$pdf->Output('Output.pdf', 'I');
}
function convertHTMLAndOutput($html) {
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream();
}
doConvert();
?>