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,297,850
Community Members
 
Community Events
165
Community Groups

Scriptrunner (confluence) - Export page to pdf, then email to others

Version: 6.15.7

Background: I'm a new(ish) jira admin who just got into scriptrunner and am having a really difficult time understanding what I need to do to combine all these components.

 

I'm trying to set up a script to run on a schedule, take a certain confluence page and export it as a pdf, then send that pdf in an email to various users.

 



I managed to get the email portion working using code/info from here:

Sending e-mail from Confluence with Groovy, Script... (atlassian.com)

 

Here is the documentation from the plugin I am using for the pdf export which I have not been able to get working yet as I'm stuck on the email attachment part so I can check if this works:

Export content via REST API (k15t.com)

 

 

 

My question(s) is this:

How do I run this cURL request in a script, save(?) the file somewhere that the script can use it again, then attach that file to the email in the script. 

 

This is what I currently have going, minus some details. I keep running into the error "FileNotFoundException" which from some googling and reading just says I didn't generate a file in the location it is trying to pull a file from? I think?

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.file.Path;
import java.nio.file.Paths;

import com.atlassian.confluence.pages.PageManager;
import com.atlassian.sal.api.component.ComponentLocator;
import com.atlassian.mail.Email;
import com.atlassian.mail.server.SMTPMailServer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Multipart;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import com.atlassian.confluence.mail.ConfluenceMailServerManager

import com.atlassian.confluence.user.AuthenticatedUserImpersonator
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.sal.api.component.ComponentLocator

def userAccessor = ComponentLocator.getComponent(UserAccessor)

def adminUser = userAccessor.getUserByName("admin")
assert adminUser 

AuthenticatedUserImpersonator.REQUEST_AGNOSTIC.asUser({
assert AuthenticatedUserThreadLocal.get().name == "admin"

}, adminUser)

URL website =
new URL("https://confluence.<local>.com/plugins/servlet/scroll-pdf/api/public/1/export-sync?templateId=com.k15t.scroll.pdf.default-template-confluence&pageId=41222495");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("date + '%m-%d-%Y' pageTitlePdf.pdf");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

Path p1 = Paths.get("date + '%m-%d-%Y' pageTitlePdf.pdf");
File attachment = p1.toFile();
def confluenceMailServerManager = ComponentLocator.getComponent(ConfluenceMailServerManager)
SMTPMailServer mailServer = confluenceMailServerManager.getDefaultSMTPMailServer();
if (mailServer != null) {
Email email = new Email("example@abc.com");
email.setSubject("page title date +'%m-%d-%Y'");
email.setBody("example-body");

Multipart multipart = new MimeMultipart();


MimeBodyPart attachmentBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment);
attachmentBodyPart.setDataHandler(new DataHandler(source));

attachmentBodyPart.setFileName(attachment.getName());
attachmentBodyPart.setContent("hello world", "text/plain;charset=utf-8");

multipart.addBodyPart(attachmentBodyPart);

email.setMultipart(multipart);

mailServer.send(email);
} else {
throw new RuntimeException("no mail server!!!");
}

 

 

Any guidance or links to relevant questions would be greatly appreciated, I've spent way more time on this than I should have already

 

 

 

 

1 answer

0 votes
Fabian Lim Community Leader Jan 14, 2022

Hi @Jordan Hauser

If you are still having issues, I suggest you contact adaptivist support directly if you are paying for this app.

Good luck. 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

91 views 0 0
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you