forked from bmg1919/Booktype-py3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooktype2mpdf.php
More file actions
112 lines (83 loc) · 3.01 KB
/
Copy pathbooktype2mpdf.php
File metadata and controls
112 lines (83 loc) · 3.01 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
<?php
error_reporting();
/* Parse the arguments */
$shortopts = "m:d:o:";
$longopts = array(
"mpdf:",
"dir:",
"output:",
);
$options = getopt($shortopts, $longopts);
chdir($options["dir"]);
$file_input = $options["dir"] . "/body.html";
$file_output = $options["output"];
/* Include mPDF library */
// include $options["mpdf"] . "/mpdf.php";
if (file_exists($options["dir"] . "/config.json")) {
$data = file_get_contents($options["dir"] . "/config.json");
$config = json_decode($data, true);
}
/* Read content */
$html = file_get_contents($file_input);
/* Create PDF */
/*
$mpdf = new mPDF('', array($config["config"]["page_width_bleed"], $config["config"]["page_height_bleed"]), '', '',
$config["config"]["settings"]["gutter"], $config["config"]["settings"]["side_margin"],
$config["config"]["settings"]["top_margin"], $config["config"]["settings"]["bottom_margin"],
$config["config"]["settings"]["header_margin"], $config["config"]["settings"]["footer_margin"]);
*/
// $mpdf = new mPDF();
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
// Specify whether to substitute missing characters in UTF-8 (multibyte) documents
$mpdf->useSubstitutions = false;
//Disables complex table borders etc. to improve performance
$mpdf->simpleTables = true;
if (array_key_exists('mirror_margins', $config)) {
if ($config['mirror_margins']) {
// Make it DOUBLE SIDED document
$mpdf->mirrorMargins = 1;
}
}
if (array_key_exists('mpdf', $config)) {
foreach ($config['mpdf'] as $name => $value) {
$mpdf->$name = $value;
}
}
$mpdf->bleedMargin = $config["config"]["settings"]["bleed_size"];
// Don't generate Table of Contents from H elements, we use Booktype's ToC with sections and chapters
$mpdf->h2toc = array();
// Generate PDF bookmarks from H elements
$mpdf->h2bookmarks = array('H1' => 0, 'H2' => 1, 'H3' => 2);
/* Add Styling */
if (file_exists($options["dir"] . "/style.css")) {
$css_data = file_get_contents($options["dir"] . "/style.css");
$mpdf->WriteHTML($css_data, 1);
}
/* Add Frontmatter */
if (file_exists($options["dir"] . "/frontmatter.html")) {
$frontmatter_data = file_get_contents($options["dir"] . "/frontmatter.html");
$frontmatter_data = strtr($frontmatter_data, array('{TITLE}' => $config['metadata']['title']));
$mpdf->WriteHTML($frontmatter_data, 2);
}
$mpdf->WriteHTML($html, 2);
/* Add Endmatter */
if (file_exists($options["dir"] . "/endmatter.html")) {
$data = file_get_contents($options["dir"] . "/endmatter.html");
$mpdf->WriteHTML($data, 2);
}
if (array_key_exists('title', $config['metadata'])) {
if (isset($config['metadata']['title'])) {
$mpdf->SetTitle($config['metadata']['title']);
}
}
if (array_key_exists('creator', $config['metadata'])) {
if (isset($config['metadata']['creator'])) {
$mpdf->SetAuthor($config['metadata']['creator']);
}
}
$mpdf->InsertIndex(1, 1);
$mpdf->SetCreator('Booktype');
$mpdf->Output($file_output);
echo '{"status": 1, "pages": ' . $mpdf->page . '}';
exit;