-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
95 lines (80 loc) · 2.93 KB
/
index.php
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
<?php
#
# To use this, you need to post to this as a url, with post input named
# 'htmlstore' and for titling the file, use 'title'
#
# NOTE: You need a custom compiled version of wkhtmltodpdf with qt that
# does not need X server, so statically compiled is the best
$html2pdf = '/app/bin/wkhtmltopdf';
$getdate = date('Y-m-d-H_i_s');
$gethash = md5(date('Ymdgisu'));
$doDebug = false;
$encoding = "utf-8";
$error_message = '<h4>Your PDF file could not be generated. Please contact <a href="mailto:[email protected]">[email protected]</a>.</h4>';
$html = '';
// if htmlstore exists, get it, if need more than one type, then
// only get one of the types sent over
if ( !empty($_REQUEST['htmlstore']) )
$html = $_REQUEST['htmlstore'];
elseif ( !empty($_REQUEST['type']) &&
isset($_REQUEST['htmlstore-' . $_REQUEST['type']]) )
{
$html = $_REQUEST['htmlstore-' . $_REQUEST['type']];
}
$filename = make_seo_url($_REQUEST['title']) . "-" . $getdate;
$file_html = "/tmp/" . $filename . ".html";
$file_pdf = "/tmp/" . $filename . ".pdf";
if ( $doDebug )
{
echo "<pre>";
var_dump($_REQUEST);
var_dump($html);
echo "</pre>";
echo "<p>$file_html</p>";
echo "<p>$file_pdf</p>";
echo $title;
echo $html;
exit;
}
if ( empty($html) || ( FALSE !== file_put_contents($file_html, $html) ) )
{
// This is the command used
// encoding is set fixed to utf-8 for now
$cmd = escapeshellcmd("$html2pdf --encoding '$encoding' '$file_html' '$file_pdf'");
exec($cmd);
if (file_exists($file_pdf))
{
$filename = $filename . ".pdf";
$file_size = filesize($file_pdf);
// Output headers.
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Length: ".$file_size);
// Output file.
ob_clean();
flush();
readfile ($file_pdf);
exit();
}
else {
die($error_message);
}
} else {
echo $error_message;
}
function make_seo_url ($string) {
//Lower case everything
$string = strtolower($string);
//Make alphanumeric (removes all other characters)
$string = preg_replace("/[^a-z0-9_\.\s-]/", "", $string);
//Clean up multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
?>