package jPDFWebSamples.cli; import java.io.IOException; import com.qoppa.pdf.PDFException; import com.qoppa.pdfWeb.PDFWeb; import com.qoppa.pdfWeb.SVGOptions; /** * Sample CLI implemenation of jPDFWeb. This simple CLI will take an input PDF file and convert it to an SVG output file. The sample * can take an optional page index to convert a snigle page. The sample can be enhanced to add other options. * * @author Qoppa Software * */ public class PDFWebCLI { public static void main(String[] args) { try { // Parse the arguments JobInfo jobInfo = parseArgs(args); jobInfo.validate(); // Check for the key if (jobInfo.mLicenseKey != null) { PDFWeb.setKey(jobInfo.mLicenseKey); } // load the document PDFWeb pdfweb = new PDFWeb(jobInfo.mInputFile, null); // Create SVGOptions SVGOptions options = new SVGOptions(); // Options can be modified here by adding command line arguments // Perform conversion if (jobInfo.mPageIndex != -1) { pdfweb.savePageAsSVG(jobInfo.mPageIndex, options, jobInfo.mOutputFile); } else { pdfweb.saveDocumentAsSVG(jobInfo.mOutputFile, options); } } catch(CLIException cliE) { cliE.printStackTrace(); } catch (PDFException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private static JobInfo parseArgs(String[] args) throws CLIException { if (args.length < 2) { printUsage(); System.exit(0); } // Create new job info JobInfo jobInfo = new JobInfo(); // Loop through the arguments int ix = 0; while (ix < args.length) { if ("-input".equalsIgnoreCase(args[ix])) { if (args.length <= ix+1) throw new CLIException("Not enough arguments"); jobInfo.mInputFile = args[ix+1]; ix += 2; } else if ("-output".equalsIgnoreCase(args[ix])) { jobInfo.mOutputFile = args[ix+1]; ix += 2; } else if ("-lickey".equalsIgnoreCase(args[ix])) { jobInfo.mLicenseKey = args[ix+1]; ix += 2; } else if ("-pageix".equalsIgnoreCase(args[ix])) { jobInfo.mPageIndex = Integer.parseInt(args[ix+1]); ix += 2; } else { throw new CLIException("Unrecognized command line option: " + args[ix]); } } return jobInfo; } private static void printUsage() { System.out.println("Usage: PDFWebCLI -input -output -lickey "); System.out.println(); System.out.println("Required arguments:"); System.out.println("-input Input file name."); System.out.println("-output Output file name."); System.out.println(); System.out.println("-pageix Page index (0 based) to convert a single page."); System.out.println("-lickey License key to run in production mode"); } }