class-pdf.php
7.64 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
<?php
/**
* This class will init the mPDF and do ser the output, though the PDF content will be
* processed in \LearnDash_Certificate_Builder\Component\Pdf\Content_Builder.
*
* @file
* @package Learndash_Certificate_Builer
*/
namespace LearnDash_Certificate_Builder\Component;
use Certificate_Builder\Traits\IO;
use LearnDash_Certificate_Builder\Component\Pdf\Pdf_Content;
use LearnDash_Certificate_Builder\Controller\Fonts_Manager;
use Mpdf\Config\FontVariables;
use Mpdf\HTMLParserMode;
use Mpdf\Mpdf;
use Mpdf\Output\Destination;
/**
* Class PDF
*
* @package LearnDash_Certificate_Builder\Component
*/
class PDF {
use IO;
/**
* The MDPF instance
*
* @var Mpdf
*/
private $mpdf;
/**
* Enable debug for element border
*
* @var bool
*/
private $debug = false;
/**
* Course ID, use in generating
*
* @var int
*/
private $course_id = 0;
/**
* PDF init.
*
* @throws \Mpdf\MpdfException Throw error if Mpdf cant not be init, should never be here.
*/
public function init() {
if ( $this->mpdf instanceof Mpdf ) {
return $this->mpdf;
}
$this->mpdf = new Mpdf(
array(
'tempDir' => $this->get_working_path(),
// by default, as it will be auto override when we define custom font.
'default_font' => 'freeserif',
'margin_left' => 0,
'margin_right' => 0,
'margin_top' => 0,
'margin_bottom' => 0,
'debug' => $this->debug,
'fontdata' => array(
'freesans' => array(
'R' => 'FreeSans.ttf',
'B' => 'FreeSansBold.ttf',
'I' => 'FreeSansOblique.ttf',
'BI' => 'FreeSansBoldOblique.ttf',
'useOTL' => 0xFF,
),
'freeserif' => array(
'R' => 'FreeSerif.ttf',
'B' => 'FreeSerifBold.ttf',
'I' => 'FreeSerifItalic.ttf',
'BI' => 'FreeSerifBoldItalic.ttf',
'useOTL' => 0xFF,
'useKashida' => 75,
),
'freemono' => array(
'R' => 'FreeMono.ttf',
'B' => 'FreeMonoBold.ttf',
'I' => 'FreeMonoOblique.ttf',
'BI' => 'FreeMonoBoldOblique.ttf',
),
),
)
);
// fallback to freesans if any font corrupt.
$this->mpdf->backupSubsFont = array( 'freesans' );
$user_fonts = get_option( Fonts_Manager::OPTION_NAME, array() );
foreach ( $user_fonts as $key => $font ) {
foreach ( $font as $k => &$url ) {
if ( in_array( $k, array( 'R', 'B', 'I', 'BI' ), true ) ) {
$url = pathinfo( $url, PATHINFO_BASENAME );
$this->mpdf->available_unifonts[] = $key . trim( $k, 'R' );
} else {
unset( $font[ $k ] );
}
}
$this->mpdf->fontdata[ $key ] = $font;
}
$this->mpdf->default_available_fonts = $this->mpdf->available_unifonts;
$this->mpdf->AddFontDirectory( $this->get_user_font_path() );
}
/**
* Get all the fonts that supported
*
* @return array
*/
public function get_fonts() {
$configs = ( new FontVariables() )->getDefaults();
$font_data = $configs['fontdata'];
$dictionary = $this->get_mpdf_fonts();
foreach ( $font_data as $key => &$data ) {
if ( ! isset( $dictionary[ $key ] ) ) {
unset( $font_data[ $key ] );
continue;
}
$name = isset( $dictionary[ $key ] ) ? $dictionary[ $key ] : $key;
$data['name'] = $name;
}
$user_fonts = get_option( Fonts_Manager::OPTION_NAME, array() );
$font_data = array_merge( $font_data, $user_fonts );
// move the freeserif to top.
$free_serif = $font_data['freeserif'];
unset( $font_data['freeserif'] );
return array_merge( array( 'freeserif' => $free_serif ), $font_data );
}
/**
* Output the pdf to screen
*
* @param array $blocks The blocks that been added.
* @param int $cert_id Certificate ID.
* @param int $course_id Course ID.
*/
public function serve( $blocks, $cert_id = 0, $course_id = 0 ) {
$this->init();
$builder = new Pdf_Content( $blocks, $this->mpdf );
$builder->course_id = $course_id;
$builder->default_colors_palette = $this->get_default_pallete_colors();
$builder->init_colors();
$this->mpdf->setMBencoding( 'UTF-8' );
$this->mpdf->usingCoreFont = false;
$this->mpdf->onlyCoreFonts = false;
$this->mpdf = apply_filters( 'learndash_certificate_builder_mpdf', $this->mpdf );
$builder->build_html_structure();
$gutenberg_style = file_get_contents( learndash_certificate_builder_path( 'src/component/pdf/style.css' ) );
$this->mpdf->WriteHTML( $gutenberg_style, HTMLParserMode::HEADER_CSS );
$this->mpdf->WriteHTML( $builder->style_builder->output(), HTMLParserMode::HEADER_CSS );
$this->mpdf->WriteHTML( sprintf( '<div id="wrap">%s</div>', $builder->html_builder->output() ) );
$pdf_name = apply_filters( 'learndash_certificate_builder_pdf_name', $this->build_pdf_file_name( $cert_id, $course_id ), $cert_id, $course_id );
$destination = apply_filters( 'learndash_certificate_builder_pdf_output_mode', Destination::INLINE, $cert_id, $course_id );
$this->mpdf->Output( $pdf_name, $destination );
}
/**
* Build the file name
*
* @param int $certificate_id The certificate ID.
* @param int $course_id The course ID.
*
* @return string
*/
private function build_pdf_file_name( $certificate_id = 0, $course_id = 0 ) {
if ( 0 !== $course_id && 0 !== $certificate_id ) {
// we only need post title.
$cert = get_post( $certificate_id );
$course = get_post( $course_id );
$user = wp_get_current_user();
return sanitize_file_name( $user->user_login . ' ' . $course->post_title . ' ' . $cert->post_title . ' ' . get_bloginfo( 'name' ) ) . '.pdf';
} else {
return 'preview.pdf';
}
}
/**
* The default color palette
*
* @return array[]
*/
public function get_default_pallete_colors() {
return array(
array(
'name' => __( 'Black', 'learndash-certificate-builder' ),
'slug' => 'black',
'color' => '#000000',
),
array(
'name' => __( 'Cyan bluish gray', 'learndash-certificate-builder' ),
'slug' => 'cyan-bluish-gray',
'color' => '#abb8c3',
),
array(
'name' => __( 'White', 'learndash-certificate-builder' ),
'slug' => 'white',
'color' => '#ffffff',
),
array(
'name' => __( 'Pale pink', 'learndash-certificate-builder' ),
'slug' => 'pale-pink',
'color' => '#f78da7',
),
array(
'name' => __( 'Vivid red', 'learndash-certificate-builder' ),
'slug' => 'vivid-red',
'color' => '#cf2e2e',
),
array(
'name' => __( 'Luminous vivid orange', 'learndash-certificate-builder' ),
'slug' => 'luminous-vivid-orange',
'color' => '#ff6900',
),
array(
'name' => __( 'Luminous vivid amber', 'learndash-certificate-builder' ),
'slug' => 'luminous-vivid-amber',
'color' => '#fcb900',
),
array(
'name' => __( 'Light green cyan', 'learndash-certificate-builder' ),
'slug' => 'light-green-cyan',
'color' => '#7bdcb5',
),
array(
'name' => __( 'Vivid green cyan', 'learndash-certificate-builder' ),
'slug' => 'vivid-green-cyan',
'color' => '#00d084',
),
array(
'name' => __( 'Pale cyan blue', 'learndash-certificate-builder' ),
'slug' => 'pale-cyan-blue',
'color' => '#8ed1fc',
),
array(
'name' => __( 'Vivid cyan blue', 'learndash-certificate-builder' ),
'slug' => 'vivid-green-cyan',
'color' => '#00d084',
),
array(
'name' => __( 'Vivid purple', 'learndash-certificate-builder' ),
'slug' => 'vivid-purple',
'color' => '#9b51e0',
),
);
}
/**
* Return a list of native supported fonts
*
* @return array
*/
private function get_mpdf_fonts() {
return array(
'freeserif' => 'Free Serif',
'freesans' => 'Free Sans',
'freemono' => 'Free Mono',
);
}
}