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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,553,234
Community Members
 
Community Events
184
Community Groups

Convert images to PDF in postfuction

Edited
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.
Mar 20, 2023

Article!

 

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 https://mvnrepository.com/artifact/com.itextpdf/itextpdf/5.5.13.2 

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 org.apache.commons.io.FileUtils
import org.apache.commons.io.FilenameUtils
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 = ImageIO.read(imageFile)
// 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
document.open()

// 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
document.add(pdfImage)
// Close the PDF document
document.close()
}

// 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
tempFile.deleteOnExit()
// 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(tempFile.name).toLowerCase()
// 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)
break
}
// Create a new PDF attachment for the converted PDF file
attachmentManager.createAttachment(pdfFile, pdfFile.name, "application/pdf", ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue)
// Delete the original image attachment
attachmentManager.deleteAttachment(attachment)
}
}
}

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

// Call the main function
main()

0 comments

Comment

Log in or Sign up to comment