|
| 1 | +package com.convertapi.examples; |
| 2 | + |
| 3 | +import com.convertapi.client.Config; |
| 4 | +import com.convertapi.client.ConversionResult; |
| 5 | +import com.convertapi.client.ConvertApi; |
| 6 | +import com.convertapi.client.Param; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.nio.file.Paths; |
| 12 | +import java.util.concurrent.CompletableFuture; |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | + |
| 15 | +import static java.lang.System.getenv; |
| 16 | + |
| 17 | +/** |
| 18 | + * Example of saving Word docx to PDF using alternative OpenOffice converter |
| 19 | + * Conversion is made by using same file parameter and processing two conversions simultaneously |
| 20 | + * https://www.convertapi.com/docx-to-pdf |
| 21 | + * https://www.convertapi.com/docx-to-png |
| 22 | + */ |
| 23 | +public class AlternativeConverter { |
| 24 | + public static void main(String[] args) throws IOException, ExecutionException, InterruptedException { |
| 25 | + Config.setDefaultSecret(getenv("CONVERTAPI_SECRET")); //Get your secret at https://www.convertapi.com/a |
| 26 | + Path tempDir = Paths.get(System.getProperty("java.io.tmpdir")); |
| 27 | + |
| 28 | + System.out.println("Converting DOCX to PDF with OpenOffice converter"); |
| 29 | + Param docxFileParam = new Param("file", new File(AlternativeConverter.class.getClassLoader().getResource("test.docx").getFile()).toPath()); |
| 30 | + Param converterParam = new Param("converter", "openofficetopdf"); |
| 31 | + |
| 32 | + CompletableFuture<ConversionResult> pdfResult = ConvertApi.convert("docx", "pdf", docxFileParam, converterParam); |
| 33 | + |
| 34 | + System.out.println("PDF file saved to: " + pdfResult.get().saveFile(tempDir).get()); |
| 35 | + } |
| 36 | +} |
0 commit comments