N/A Download pdf from orchestrator

newdeveloper55

Member
I would like to download a PDF after creating it from a base 64 string. I also tried with the PdfBox module but I can't use it. Do you know how I can solve it?

Here is the last code I wanted to use but which gives me an error:

import groovy.transform.CompileStatic;
import com.oracle.e1.common.OrchestrationAttributes;
import java.sql.*;
import java.util.Base64
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import javax.servlet.http.HttpServletResponse

@CompileStatic
HashMap<String, Object> main(OrchestrationAttributes orchAttr, Connection sqlConnection, HashMap inputMap)
{
HashMap<String, Object> returnMap = new HashMap<String, Object>();


def base64EncodedPDF = "base 64 code"

def pdfBytes = Base64.getDecoder().decode(base64EncodedPDF)
def outputStream = new ByteArrayInputStream(pdfBytes)

PdfController.createPdfAndDownload(response, outputStream)

return returnMap;
}


class PdfController {
static void createPdfAndDownload(HttpServletResponse response, ByteArrayInputStream inputStream) {
// Imposta i content type e gli header per indicare al browser che è un file PDF da scaricare
response.setContentType("application/pdf")
response.setHeader("Content-Disposition", "attachment; filename=output.pdf")

// Scrivi i byte del PDF nella risposta
byte[] buffer = new byte[4096]
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
response.getOutputStream().write(buffer, 0, bytesRead)
}
inputStream.close()
response.getOutputStream().flush()
response.getOutputStream().close()
}
}
 
I've implemented a solution, try with this code:

Java:
import com.oracle.e1.common.OrchestrationAttributes;
import java.text.SimpleDateFormat;

import org.apache.commons.codec.binary.Base64
import java.nio.file.Files
import java.nio.file.Paths

HashMap<String, Object> main(OrchestrationAttributes orchAttr, HashMap inputMap)
{
  HashMap<String, Object> returnMap = new HashMap<String, Object>();
    
    // Add logic here
    String base64PDF  = (String)inputMap.get("base64PDF");
    String outputPath  = (String)inputMap.get("outputPath");

    try{
        def binaryData = Base64.decodeBase64(base64PDF);

       // Write binary data to the PDF file
        Files.write(Paths.get(outputPath), binaryData);
    
        returnMap.put("result", "File Created!");
    }catch(Exception e){
        returnMap.put("result", "Error: " + e);
    }
    

  return returnMap;
}

I hope help you.

Best regards.
 
Back
Top