-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathTcpdfTestCase.php
More file actions
65 lines (57 loc) · 1.78 KB
/
Copy pathTcpdfTestCase.php
File metadata and controls
65 lines (57 loc) · 1.78 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
<?php
declare(strict_types=1);
/**
* Shared helpers for the TCPDF facade test suite.
*
* @package com.tecnick.tcpdf
*/
use PHPUnit\Framework\TestCase;
abstract class TcpdfTestCase extends TestCase
{
/**
* Create a document with sane test defaults (no header/footer).
*/
protected function newPdf(string $orientation = 'P', string $unit = 'mm', mixed $format = 'A4'): TCPDF
{
$pdf = new TCPDF($orientation, $unit, $format, true, 'UTF-8', false);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->setCompression(true);
return $pdf;
}
/**
* Run a poppler tool on the rendered document and return its output.
*/
protected function popplerTool(string $tool, TCPDF $pdf, string $args = ''): string
{
$raw = $pdf->getPDFData();
$this->assertIsString($raw);
$this->assertStringStartsWith('%PDF-', $raw);
$tmpfile = tempnam(sys_get_temp_dir(), 'tcpdf_test_');
$this->assertNotFalse($tmpfile);
file_put_contents($tmpfile, $raw);
try {
$lines = [];
$code = 1;
exec($tool . ' ' . escapeshellarg($tmpfile) . ' ' . $args . ' 2>/dev/null', $lines, $code);
$this->assertSame(0, $code, $tool . ' must parse the document');
return implode("\n", $lines);
} finally {
unlink($tmpfile);
}
}
/**
* Extract the text content of the rendered document.
*/
protected function extractText(TCPDF $pdf): string
{
return $this->popplerTool('pdftotext', $pdf, '-');
}
/**
* Return the pdfinfo report of the rendered document.
*/
protected function pdfInfo(TCPDF $pdf): string
{
return $this->popplerTool('pdfinfo', $pdf);
}
}