Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Convert images to PDF in postfuction

Ilya Stekolnikov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2023



The code provided is a script written in Groovy programming language (Scriptrunner plugin) that converts image attachments of a JIRA issue into PDF format. It makes use of several libraries and APIs, including the Atlassian JIRA API, Apache Commons IO, iText PDF library, and JSoup HTML parser.

The convertImageToPdf() function is responsible for converting a given image file into a PDF file. It reads the image file using ImageIO library, creates a new PDF document using iText library, and adds the image to the document using Image class. The resulting PDF file is then saved to the specified path.

The processAttachments() function is the main function that processes all the attachments of a given JIRA issue. It retrieves the list of attachments using the JIRA API, iterates over them, and checks if the attachment is an image (JPEG, JPG, or PNG). If it is, it calls the convertImageToPdf() function to convert the image to PDF format, creates a new PDF attachment using the converted PDF file, and deletes the original image attachment.

Finally, the main() function is the entry point of the script. It calls the processAttachments() function for the current issue.

In summary, this script can be useful for teams who want to convert image attachments to PDF format for easier sharing and collaboration. It can be customized and extended to support other file formats and JIRA operations.

Also in code i use this lib 

download it to (after downloading you should restart jira)

  • Server Jira: JIRA_INSTALL/atlassian-jira/WEB-INF/lib
  • Data Center Jira: JIRA_INSTALL/WEB-INF/lib
Here is the code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import java.awt.image.BufferedImage
import com.itextpdf.text.Document
import com.itextpdf.text.Image
import com.itextpdf.text.PageSize
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.tool.xml.XMLWorkerHelper
import org.jsoup.Jsoup
import com.itextpdf.text.FontFactory
import com.itextpdf.text.pdf.BaseFont
import java.nio.charset.StandardCharsets

// Converts the given image file to a PDF file
def convertImageToPdf(imageFile, pdfFile) {
// Read the image file as a BufferedImage
BufferedImage image =
// Create a new PDF document with A4 page size
Document document = new Document(PageSize.A4, 0, 0, 0, 0)
// Create a PdfWriter to write the PDF to the output stream
PdfWriter.getInstance(document, new FileOutputStream(pdfFile))
// Open the PDF document

// Create an iText Image object from the image file
Image pdfImage = Image.getInstance(imageFile.path)
// Scale the image to fit the A4 page size
pdfImage.scaleToFit(PageSize.A4.width, PageSize.A4.height)
// Add the image to the PDF document
// Close the PDF document

// Processes the attachments of the given JIRA issue
def processAttachments(Issue issue) {
// Get the AttachmentManager instance
def attachmentManager = ComponentAccessor.getAttachmentManager()
// Get the list of attachments for the issue
def attachments = attachmentManager.getAttachments(issue)

// Iterate over each attachment
attachments.each { Attachment attachment ->
// Create a temporary file for the attachment
def tempFile = File.createTempFile("attachment-", "-" + attachment.filename)
// Mark the temporary file for deletion when the JVM exits
// Stream the attachment content to the temporary file
attachmentManager.streamAttachmentContent(attachment, { inputStream ->
FileUtils.copyInputStreamToFile(inputStream, tempFile)

// Get the file extension of the attachment
String extension = FilenameUtils.getExtension(
// Check if the attachment is an image file (JPEG, JPG, or PNG)
if (["jpg", "jpeg", "png"].contains(extension)) {
// Create a new PDF file for the converted attachment
def pdfFile = new File(tempFile.path + ".pdf")
// Convert the image attachment to PDF format
switch (extension) {
case ["jpg", "jpeg", "png"]:
convertImageToPdf(tempFile, pdfFile)
// Create a new PDF attachment for the converted PDF file
attachmentManager.createAttachment(pdfFile,, "application/pdf", ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue)
// Delete the original image attachment

// The entry point of the script
def main() {
// Get the current issue
Issue issue = issue
// Process the attachments of the issue

// Call the main function



Log in or Sign up to comment
AUG Leaders

Atlassian Community Events