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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales