Tips & Tricks - Convert PDF to image file
This workshop demonstrates how PDF files, which are saved as attachments in an application, can be converted into image files (e.g. .png or .jpg) via a process. You can download the sample application with process here and import it as usual. Activate the included process afterwards.
Application
The example application contains an edit page where a title and description can be entered. Furthermore, there is a file control for uploading PDFs in the browser. The images created from the PDFs are stored in the subordinate data group "images".
The data type of the primary key of this data group is GUID.
In the "images" data group there is a file data field in which the reference to the created images is saved by the process.
Process
The process reacts to inserting or changing an entry in the "PDFs" data group.
Afterwards, the images already stored in the child table should be deleted, in case an existing PDF is exchanged.
Then, the PDF file is broken down into images with a Groovy script action. The conversion of individual PDF pages to images is carried out with the Apache project "PDFBox"(https://pdfbox.apache.org). All necessary classes are included with the installation of Intrexx and only need to be integrated as an import in the Groovy script.
import java.awt.toolkit import java.awt.image.BufferedImage import org.apache.pdfbox.pdmodel.* import org.apache.pdfbox.loader import org.apache.pdfbox.rendering.PDFRenderer import java.io.IOException import org.apache.pdfbox.pdmodel.PDPage import org.apache.pdfbox.pdmodel.font.PDType1Font import org.apache.pdfbox.pdmodel.PDPageContentStream import javax.imageio.ImageIO import java.util.List import java.io.File import java.io.FileWriter import java.io.BufferedWriter import org.apache.pdfbox.rendering.ImageType //define variables def conn = g_dbConnections.systemConnection def strNewGuid def l_pdfPage = 1 File tempFile = null def inputFile = g_record[strInputFileGuid] /* datafield file <file> */ int l_recId = g_record[strRecIdGuid].value /* datafield (PK) (S) ID <integer> */ if(inputFile.hasValue() && inputFile.getFirstFile().getContentType()=="application/pdf"){ try { //get file from Intrexx def pdfFile = new File(inputFile.getFirstFile().getPath()) // load PDF document PDDocument document = Loader.loadPDF(pdfFile) // get all pages pages = document.getDocumentCatalog().getPages() PDFRenderer pdfRenderer = new PDFRenderer(document); // for each page for (int i = 0; i < pages.getCount(); i++) { // single page PDPage singlePage = pages.get(i) // to BufferedImage BufferedImage buffImage = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB); /*use for quality configuration*/ //BufferedImage buffImage = singlePage.convertToImage(BufferedImage.TYPE_INT_BGR, 70) // write image to temporary file tempFile = File.createTempFile("pdfImage", ".png") ImageIO.write(buffImage, "png", tempFile) //generate new Guid for insert query strNewGuid = newGuid() //insert into child table g_dbQuery.executeUpdate(conn, "INSERT INTO ${strImageDatagroup} (${strVariableFKLID}, ${strVariableImageRecId}, ${strVariablePage}) VALUES (?, ?, ?)") { setInt(1, l_recId) setString(2, strNewGuid) setInt(3, l_pdfPage) } //save image to Intrexx g_dgFile.copy(guid: strImageFileGuid, id: strNewGuid, file: tempFile.getAbsolutePath(), replaceMode: true, triggerWorkflow: false) //next l_pdfPage as integer l_pdfPage = l_pdfPage +1 tempFile.delete() } document.close() } catch (IOException e) { g_log.err(e) } finally { //delete pdfbox temp files File path = new File("internal/tmp/") for (File file : path.listFiles()) { if (file.getName().endsWith(".tmp") && file.getName().startsWith("+~")) { file.delete() } } } }