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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales