Convert Between File Types - Python Sample Code

Sample code to use Apryse Server SDK for direct, high-quality conversion between PDF, XPS, SVG, TIFF, PNG, JPEG, and other image formats ('pdftron.PDF.Convert' namespace); provided in Python, C++, C#, Java, JavaScript, PHP, Ruby, Go and VB. The sample also shows how to convert MS Office files using our built in conversion. Learn more about our Server SDK and PDF Conversion Library.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11import platform
12
13sys.path.append("../../LicenseKey/PYTHON")
14from LicenseKey import *
15
16#---------------------------------------------------------------------------------------
17# The following sample illustrates how to use the PDF::Convert utility class to convert
18# documents and files to PDF, XPS, or SVG, or EMF. The sample also shows how to convert MS Office files
19# using our built in conversion.
20#
21# Certain file formats such as XPS, EMF, PDF, and raster image formats can be directly
22# converted to PDF or XPS.
23#
24# Please contact us if you have any questions.
25#---------------------------------------------------------------------------------------
26
27# Relative path to the folder containing the test files.
28inputPath = "../../TestFiles/"
29outputPath = "../../TestFiles/Output/"
30
31
32
33def ConvertSpecificFormats():
34 ret = 0
35 try:
36 # Start with a PDFDoc to collect the converted documents
37 pdfdoc = PDFDoc()
38 s1 = inputPath + "simple-xps.xps"
39
40 # Convert the XPS document to PDF
41 print("Converting from XPS")
42 Convert.FromXps(pdfdoc, s1)
43 outputFile = "xps2pdf v2.pdf"
44 pdfdoc.Save(outputPath + outputFile, SDFDoc.e_remove_unused)
45 print("Saved " + outputFile)
46
47
48 # Convert the TXT document to PDF
49 set = ObjSet()
50 options = set.CreateDict()
51 # Put options
52 options.PutNumber("FontSize", 15)
53 options.PutBool("UseSourceCodeFormatting", True)
54 options.PutNumber("PageWidth", 12)
55 options.PutNumber("PageHeight", 6)
56 s1 = inputPath + "simple-text.txt"
57 print("Converting from txt")
58 Convert.FromText(pdfdoc, s1)
59 outputFile = "simple-text.pdf"
60 pdfdoc.Save(outputPath + outputFile, SDFDoc.e_remove_unused)
61 print("Saved " + outputFile)
62
63 # Convert the two page PDF document to SVG
64 outputFile = "pdf2svg v2.svg"
65 pdfdoc = PDFDoc(inputPath + "newsletter.pdf")
66 print("Converting pdfdoc to SVG")
67 Convert.ToSvg(pdfdoc, outputPath + outputFile)
68 print("Saved " + outputFile)
69
70 # Convert the PNG image to XPS
71 print("Converting PNG to XPS")
72 outputFile = "butterfly.xps"
73 Convert.ToXps(inputPath + "butterfly.png", outputPath +outputFile)
74 print("Saved " + outputFile)
75
76 # Convert PDF document to XPS
77 print("Converting PDF to XPS")
78 outputFile = "newsletter.xps"
79 Convert.ToXps(inputPath + "newsletter.pdf", outputPath + outputFile)
80 print("Saved " + outputFile)
81
82 # Convert PDF document to HTML
83 print("Converting PDF to HTML")
84 outputFile = "newsletter"
85 Convert.ToHtml(inputPath + "newsletter.pdf", outputPath + outputFile)
86 print("Saved newsletter as HTML")
87
88 # Convert PDF document to EPUB
89 print("Converting PDF to EPUB")
90 outputFile = "newsletter.epub"
91 Convert.ToEpub(inputPath + "newsletter.pdf", outputPath + outputFile)
92 print("Saved " + outputFile)
93
94 print("Converting PDF to multipage TIFF")
95 tiff_options = TiffOutputOptions()
96 tiff_options.SetDPI(200)
97 tiff_options.SetDither(True)
98 tiff_options.SetMono(True)
99 Convert.ToTiff(inputPath + "newsletter.pdf", outputPath + "newsletter.tiff", tiff_options)
100 print("Saved newsletter.tiff")
101
102 # Convert SVG file to PDF
103 print("Converting SVG to PDF")
104 pdfdoc = PDFDoc()
105 Convert.FromSVG(pdfdoc, inputPath + "tiger.svg")
106 pdfdoc.Save(outputPath + "svg2pdf.pdf", SDFDoc.e_remove_unused)
107 print("Saved svg2pdf.pdf")
108
109 except:
110 ret = 1
111 return ret
112
113# convert from a file to PDF automatically
114def ConvertToPdfFromFile():
115 testfiles = [
116 [ "simple-word_2007.docx","docx2pdf.pdf"],
117 [ "simple-powerpoint_2007.pptx","pptx2pdf.pdf"],
118 [ "simple-excel_2007.xlsx","xlsx2pdf.pdf"],
119 [ "simple-text.txt","txt2pdf.pdf"],
120 [ "butterfly.png","png2pdf.pdf"],
121 [ "simple-xps.xps","xps2pdf.pdf"],
122 ]
123 ret = 0
124
125
126 for testfile in testfiles:
127 try:
128 pdfdoc = PDFDoc()
129 inputFile = testfile[0]
130 outputFile = testfile[1]
131 Printer.SetMode(Printer.e_prefer_builtin_converter)
132
133 Convert.ToPdf(pdfdoc, inputPath + inputFile)
134 pdfdoc.Save(outputPath + outputFile, SDFDoc.e_linearized)
135 pdfdoc.Close()
136 print("Converted file: " + inputFile + "\nto: " + outputFile)
137 except:
138 ret = 1
139 print("ERROR: on input file " + inputFile)
140 return ret
141
142
143def main():
144 # The first step in every application using PDFNet is to initialize the
145 # library. The library is usually initialized only once, but calling
146 # Initialize() multiple times is also fine.
147 PDFNet.Initialize(LicenseKey)
148
149 # Demonstrate Convert.ToPdf and Convert.Printer
150 err = ConvertToPdfFromFile()
151 if err:
152 print("ConvertFile failed")
153 else:
154 print("ConvertFile succeeded")
155
156 # Demonstrate Convert.[FromEmf, FromXps, ToEmf, ToSVG, ToXPS]
157 err = ConvertSpecificFormats()
158 if err:
159 print("ConvertSpecificFormats failed")
160 else:
161 print("ConvertSpecificFormats succeeded")
162
163
164 PDFNet.Terminate()
165 print("Done.")
166
167if __name__ == '__main__':
168 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales