forked from pdfcrowd/pdfcrowd-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
127 lines (116 loc) · 5.2 KB
/
tests.py
File metadata and controls
127 lines (116 loc) · 5.2 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
#!/usr/bin/env python
# Copyright (C) 2009-2013 pdfcrowd.com
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
if __name__ == "__main__":
import sys
import os
import pdfcrowd
if len(sys.argv) < 2:
print "usage: python pdfcrowd.py username apikey [apihost [http-port https-port]]"
sys.exit(1)
if len(sys.argv) > 3:
pdfcrowd.HOST = sys.argv[3]
if len(sys.argv) == 6:
pdfcrowd.HTTP_PORT = int(sys.argv[4])
pdfcrowd.HTTPS_PORT = int(sys.argv[5])
print "using %s ports %d %d" % (pdfcrowd.HOST, pdfcrowd.HTTP_PORT, pdfcrowd.HTTPS_PORT)
os.chdir(os.path.dirname(sys.argv[0]))
test_dir = './test_files'
def out_stream(name, use_ssl):
fname = test_dir + '/out/py_client_%s' % name
if use_ssl:
fname = fname + '_ssl'
return open(fname + '.pdf', 'wb')
html="<html><body>Uploaded content!</body></html>"
client = pdfcrowd.Client(sys.argv[1], sys.argv[2])
for use_ssl in [False, True]:
client.useSSL(use_ssl)
try:
ntokens = client.numTokens()
client.setFooterText("%p out of %n")
client.convertURI('http://dl.dropboxusercontent.com/u/9346438/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()
print 'remaining tokens:', after_tokens
assert ntokens-4 == after_tokens
except pdfcrowd.Error, why:
print 'FAILED:', why
sys.exit(1)
# test individual methods
tests = (('setPageWidth', 500),
('setPageHeight', -1),
('setHorizontalMargin', 72),
('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', .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', 'Your Name'),
('setPageBackgroundColor', 'ee82EE'),
('setTransparentBackground', True)
)
try:
for method, arg in tests:
client = pdfcrowd.Client(sys.argv[1], sys.argv[2])
client.setVerticalMargin("1in")
getattr(client, method)(arg)
client.convertFile(test_dir + '/in/simple.html', out_stream(method.lower(), False))
except pdfcrowd.Error, why:
print 'FAILED', why
sys.exit(1)
# 4 margins
client = pdfcrowd.Client(sys.argv[1], sys.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
client = pdfcrowd.Client(sys.argv[1], sys.argv[2])
try:
client.setFailOnNon200(True)
client.convertURI("http://s3.pdfcrowd.com/this/url/does/not/exist/")
print "FAILED expected an exception"
sys.exit(1)
except pdfcrowd.Error, why:
pass # expected