Skip to content

Commit f7ce83b

Browse files
author
Laurynas Butkus
committed
Add word to pdf and png example
1 parent acd2e44 commit f7ce83b

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

convertapi/file_param.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ def build(resource):
1313
return resource.url
1414

1515
if isinstance(resource, UploadIO):
16-
return resource.upload()
16+
return resource.file_id
1717

1818
if isinstance(resource, FileIO):
19-
return UploadIO(resource).upload()
19+
return UploadIO(resource).file_id
2020

2121
if os.path.isfile(resource):
2222
io = open(resource, 'rb')
23-
return UploadIO(io).upload()
23+
return UploadIO(io).file_id
2424

2525
return resource

convertapi/upload_io.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44
class UploadIO:
55
def __init__(self, io, filename = None):
66
self.io = io
7-
self.__filename = filename
7+
self._filename = filename
8+
self._file_id = None
89

9-
def upload(self):
10-
result = convertapi.client.upload(self.io, self.filename)
11-
return result['FileId']
10+
@property
11+
def file_id(self):
12+
if self._file_id is None:
13+
self._file_id = self.__upload()
14+
15+
return self._file_id
1216

1317
@property
1418
def filename(self):
15-
if self.__filename:
16-
return self.__filename
19+
if self._filename:
20+
return self._filename
1721

1822
if 'name' in dir(self.io):
1923
return os.path.basename(self.io.name)
2024

2125
raise 'Filename must be provided for non File resources'
26+
27+
def __upload(self):
28+
result = convertapi.client.upload(self.io, self.filename)
29+
return result['FileId']

examples/convert_url_to_pdf.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import convertapi
2+
import os
3+
import tempfile
4+
5+
convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
6+
7+
# Example of converting Web Page URL to PDF file
8+
# https://www.convertapi.com/web-to-pdf
9+
10+
result = convertapi.convert(
11+
'pdf',
12+
{
13+
'Url': 'https://en.wikipedia.org/wiki/Data_conversion',
14+
'FileName': 'web-example',
15+
},
16+
from_format = 'web',
17+
timeout = 180,
18+
)
19+
20+
saved_files = result.save_files(tempfile.gettempdir())
21+
22+
print("The web page PDF saved to %s" % saved_files)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import convertapi
2+
import os
3+
import tempfile
4+
5+
convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret
6+
7+
# Example of saving Word docx to PDF and to PNG
8+
# https://www.convertapi.com/docx-to-pdf
9+
# https://www.convertapi.com/docx-to-png
10+
11+
# Use upload IO wrapper to upload file only once to the API
12+
upload_io = convertapi.UploadIO(open('files/test.docx', 'rb'))
13+
14+
saved_files = convertapi.convert('pdf', { 'File': upload_io }).save_files(tempfile.gettempdir())
15+
16+
print("The PDF saved to %s" % saved_files)
17+
18+
# Reuse the same uploaded file
19+
20+
saved_files = convertapi.convert('png', { 'File': upload_io }).save_files(tempfile.gettempdir())
21+
22+
print("The PNG saved to %s" % saved_files)

examples/create_pdf_thumbnail.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
# https://www.convertapi.com/pdf-to-extract
99
# https://www.convertapi.com/pdf-to-jpg
1010

11-
pdf_result = convertapi.convert('extract',
11+
pdf_result = convertapi.convert(
12+
'extract',
1213
{
1314
'File': 'files/test.pdf',
1415
'PageRange': 1,
15-
})
16+
}
17+
)
1618

1719
jpg_result = convertapi.convert(
1820
'jpg',
@@ -22,7 +24,8 @@
2224
'ScaleProportions': True,
2325
'ImageHeight': 300,
2426
'ImageWidth': 300,
25-
})
27+
}
28+
)
2629

2730
saved_files = jpg_result.save_files(tempfile.gettempdir())
2831

0 commit comments

Comments
 (0)