-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtest.php
149 lines (130 loc) · 4.49 KB
/
test.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
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
#!/usr/bin/php
<?php
// $argv .. array
// $argc
require 'pdfcrowd.php';
if ($argc < 2) {
echo "usage: test.php username api_key [apihost [http-port https-port]]\n";
exit(1);
}
if ($argv > 3) {
Pdfcrowd::$api_host = $argv[3];
}
if ($argv == 6) {
Pdfcrowd::$http_port = $argv[4];
Pdfcrowd::$https_port = $argv[5];
}
$api_host = Pdfcrowd::$api_host;
$http_port = Pdfcrowd::$http_port;
$https_port = Pdfcrowd::$https_port;
echo "using {$api_host} ports {$http_port} {$https_port}\n";
chdir(dirname($argv[0]));
$test_dir = './test_files';
function out_stream($name, $use_ssl)
{
$fname = "./test_files/out/php_client_{$name}";
if ($use_ssl)
$fname .= "_ssl";
return fopen($fname . '.pdf', 'wb');
}
$html = "<html><body>Uploaded content!</body></html>";
$client = new Pdfcrowd($argv[1], $argv[2]);
foreach(array(False, True) as $i => $use_ssl) {
$client->useSSL($use_ssl);
try
{
$ntokens = $client->numTokens();
$client->convertURI('https://storage.googleapis.com/pdfcrowd-legacy-tests/tests/webtopdfcom.html', out_stream('uri', $use_ssl));
$client->convertHtml($html, out_stream('content', $use_ssl));
$client->convertFile($test_dir . '/in/simple.html', out_stream('upload', $use_ssl));
$client->convertFile($test_dir . '/in/archive.tar.gz', out_stream('archive', $use_ssl));
$after_tokens = $client->numTokens();
echo "remaining tokens: {$after_tokens}\n";
if ($ntokens - 4 != $after_tokens) {
throw new Exception("Mismatch in the number of tokens.");
}
}
catch(PdfcrowdException $e)
{
echo "EXCEPTION: " . $e->getMessage();
exit(1);
}
}
$tests = array(
'setPageWidth' => 500,
'setPageHeight' => -1,
'setHorizontalMargin' => 0,
'setVerticalMargin' => 72,
'setEncrypted' => True,
'setUserPassword' => 'userpwd',
'setOwnerPassword' => 'ownerpwd',
'setNoPrint' => True,
'setNoModify' => True,
'setNoCopy' => True,
'setPageLayout' => Pdfcrowd::CONTINUOUS,
'setPageMode' => Pdfcrowd::FULLSCREEN,
'setFooterText' => '%p/%n | source %u',
'enableImages' => False,
'enableBackgrounds' => False,
'setHtmlZoom' => 300,
'enableJavaScript' => False,
'enableHyperlinks' => False,
'setDefaultTextEncoding' => 'iso-8859-1',
'usePrintMedia' => True,
'setMaxPages' => 1,
'enablePdfcrowdLogo' => True,
'setInitialPdfZoomType' => Pdfcrowd::FIT_PAGE,
'setInitialPdfExactZoom' => 113,
'setPdfScalingFactor' => 0.5,
'setFooterHtml' => '<b>bold</b> and <i>italic</i> <img src="http://s3.pdfcrowd.com/test-resources/logo175x30.png" />',
'setFooterUrl' => 'http://s3.pdfcrowd.com/test-resources/footer.html',
'setHeaderHtml' => 'page %p out of %n',
'setHeaderUrl' => 'http://s3.pdfcrowd.com/test-resources/header.html',
'setAuthor' => 'Custom Author',
'setPageBackgroundColor' => 'ee82EE',
'setTransparentBackground' => True,
'setUserAgent' => "test user agent"
);
try
{
foreach($tests as $method => $arg)
{
$client = new Pdfcrowd($argv[1], $argv[2]);
$client->$method($arg);
$client->setVerticalMargin('1in');
$client->convertFile($test_dir . '/in/simple.html', out_stream(strtolower($method), False));
}
}
catch(PdfcrowdException $e)
{
echo "EXCEPTION: " . $e->getMessage();
exit(1);
}
// margins
$client = new Pdfcrowd($argv[1], $argv[2]);
$client->setPageMargins('0.25in', '0.5in', '0.75in', '1.0in');
$client->convertHtml('<div style="background-color:red;height:100%">4 margins</div>', out_stream('4margins', False));
// expected failures
$failures = array(
array("convertHtml", "", "must not be empty"),
array("convertFile", "does-not-exist.html", "not found"),
array("convertFile", "/", "not a directory"),
array("convertFile", $test_dir."/in/empty.html", "must not be empty"),
array("convertURI", "domain.com", "must start with"),
array("convertURI", "HtTp://s3.pdfcrowd.com/this/url/does/not/exist/", "Received a non-2xx response")
);
$client = new Pdfcrowd($argv[1], $argv[2]);
$client->setFailOnNon200(True);
foreach($failures as $failure) {
try {
$client->$failure[0]($failure[1]);
echo "FAILED expected an exception: ${failure}\n";
exit(1);
} catch(PdfcrowdException $e) {
if (!strstr($e->getMessage(), $failure[2])) {
echo "error message [". $e->getMessage() ."] is expected to contain [".$failure[2]."]\n";
exit(1);
}
}
}
?>