View Javadoc
1   package com.acumenvelocity.ath.controller;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.InputStream;
6   
7   import javax.ws.rs.core.Response.Status;
8   
9   import com.acumenvelocity.ath.common.AthUtil;
10  import com.acumenvelocity.ath.common.ControllerUtil;
11  import com.acumenvelocity.ath.common.Log;
12  import com.acumenvelocity.ath.common.PdfUtil;
13  import com.acumenvelocity.ath.common.Response;
14  import com.acumenvelocity.ath.model.OcrMode;
15  
16  import io.swagger.oas.inflector.models.RequestContext;
17  import io.swagger.oas.inflector.models.ResponseContext;
18  import net.sf.okapi.common.LocaleId;
19  import net.sf.okapi.common.MimeTypeMapper;
20  import net.sf.okapi.common.StreamUtil;
21  
22  public class UtilityController {
23  
24    /**
25     * Convert PDF to DOCX using Adobe PDF Services
26     * 
27     * @param request     the request context
28     * @param pdfFile     the PDF file to convert
29     * @param pdfFileName the original name of the PDF file
30     * @param language    the language code (e.g., "en-US", "fr-FR")
31     * @param ocrMode     OCR mode: AUTO (default), ENABLED, or DISABLED
32     * @return ResponseContext containing the converted DOCX file or error
33     */
34    public ResponseContext convertPdfToDocx(RequestContext request, File pdfFile,
35        String pdfFileName, String language, String ocrModeValue) {
36  
37      if (!ControllerUtil.checkParam(pdfFile)) {
38        return Response.error(400, "Invalid request, pdfFile is not specified");
39      }
40  
41      if (!ControllerUtil.checkParam(pdfFileName)) {
42        return Response.error(400, "Invalid request, pdfFileName is not specified");
43      }
44  
45      if (!ControllerUtil.checkParam(language)) {
46        return Response.error(400, "Invalid request, language is not specified");
47      }
48  
49      OcrMode ocrMode;
50      
51      // Default to AUTO if not specified
52      if (ocrModeValue == null) {
53        ocrMode = OcrMode.AUTO;
54        
55      } else {
56        ocrMode = OcrMode.fromValue(ocrModeValue);
57      }
58  
59      try {
60        LocaleId locale = LocaleId.fromString(language);
61  
62        // Determine OCR mode if set to AUTO
63        if (ocrMode == OcrMode.AUTO) {
64          boolean needsOcr = PdfUtil.needsOcr(pdfFile);
65  
66          if (needsOcr) {
67            ocrMode = OcrMode.ENABLED;
68  
69            Log.info(UtilityController.class,
70                "PDF '{}' requires OCR, enabling OCR mode", pdfFileName);
71  
72          } else {
73            ocrMode = OcrMode.DISABLED;
74  
75            Log.info(UtilityController.class,
76                "PDF '{}' contains selectable text, disabling OCR mode", pdfFileName);
77          }
78        }
79  
80        Log.info(UtilityController.class,
81            "Converting PDF '{}' to DOCX with language='{}', ocrMode={}",
82            pdfFileName, language, ocrMode);
83  
84        // Convert PDF to DOCX
85        InputStream docxStream = PdfUtil.convertPdfToDocx(
86            new java.io.FileInputStream(pdfFile),
87            locale,
88            ocrMode);
89  
90        // Generate output filename by replacing .pdf extension with .docx
91        String docxFileName = pdfFileName;
92  
93        if (docxFileName.toLowerCase().endsWith(".pdf")) {
94          docxFileName = docxFileName.substring(0, docxFileName.length() - 4) + ".docx";
95  
96        } else {
97          docxFileName = docxFileName + ".docx";
98        }
99  
100       Log.info(UtilityController.class,
101           "Successfully converted PDF '{}' to DOCX '{}'",
102           pdfFileName, docxFileName);
103 
104       // Create temp file to hold the converted content
105       File outputFile = AthUtil.createTempFile();
106 
107       try (FileOutputStream fos = new FileOutputStream(outputFile)) {
108         StreamUtil.copy(docxStream, fos);
109       }
110 
111       return Response.builder()
112           .status(Status.OK)
113           .header("Content-Type",
114               MimeTypeMapper.DOCX_MIME_TYPE)
115           .header("Content-Disposition",
116               String.format("attachment; filename=\"%s\"", docxFileName))
117           .entity(outputFile)
118           .build();
119 
120     } catch (Exception e) {
121       Log.error(UtilityController.class, e,
122           "Error converting PDF '{}' to DOCX", pdfFileName);
123 
124       return Response.error(500, e,
125           "Failed to convert PDF to DOCX: " + e.getMessage());
126     }
127   }
128 
129   /**
130    * Convert DOCX to PDF using Adobe PDF Services
131    * 
132    * @param request      the request context
133    * @param docxFile     the DOCX file to convert
134    * @param docxFileName the original name of the DOCX file
135    * @param language     the language code (e.g., "en-US", "fr-FR")
136    * @return ResponseContext containing the converted PDF file or error
137    */
138   public ResponseContext convertDocxToPdf(RequestContext request, File docxFile,
139       String docxFileName, String language) {
140 
141     if (!ControllerUtil.checkParam(docxFile)) {
142       return Response.error(400, "Invalid request, docxFile is not specified");
143     }
144 
145     if (!ControllerUtil.checkParam(docxFileName)) {
146       return Response.error(400, "Invalid request, docxFileName is not specified");
147     }
148 
149     if (!ControllerUtil.checkParam(language)) {
150       return Response.error(400, "Invalid request, language is not specified");
151     }
152 
153     try {
154       LocaleId locale = LocaleId.fromString(language);
155 
156       Log.info(UtilityController.class,
157           "Converting DOCX '{}' to PDF with language='{}'",
158           docxFileName, language);
159 
160       // Convert DOCX to PDF
161       InputStream pdfStream = PdfUtil.convertDocxToPdf(
162           new java.io.FileInputStream(docxFile),
163           locale);
164 
165       // Generate output filename by replacing .docx extension with .pdf
166       String pdfFileName = docxFileName;
167 
168       if (pdfFileName.toLowerCase().endsWith(".docx")) {
169         pdfFileName = pdfFileName.substring(0, pdfFileName.length() - 5) + ".pdf";
170 
171       } else {
172         pdfFileName = pdfFileName + ".pdf";
173       }
174 
175       Log.info(UtilityController.class,
176           "Successfully converted DOCX '{}' to PDF '{}'",
177           docxFileName, pdfFileName);
178 
179       // Create temp file to hold the converted content
180       File outputFile = AthUtil.createTempFile();
181 
182       try (FileOutputStream fos = new FileOutputStream(outputFile)) {
183         StreamUtil.copy(pdfStream, fos);
184       }
185 
186       return Response.builder()
187           .status(Status.OK)
188           .header("Content-Type",
189               MimeTypeMapper.PDF_MIME_TYPE)
190           .header("Content-Disposition",
191               String.format("attachment; filename=\"%s\"", pdfFileName))
192           .entity(outputFile)
193           .build();
194 
195     } catch (Exception e) {
196       Log.error(UtilityController.class, e,
197           "Error converting DOCX '{}' to PDF", docxFileName);
198 
199       return Response.error(500, e,
200           "Failed to convert DOCX to PDF: " + e.getMessage());
201     }
202   }
203 }