Scriptrunner (confluence) - Export page to pdf, then email to others
Asked by Jordan Hauser:

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.

<br />

<br />

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

[Sending e-mail from Confluence with Groovy, Script... (atlassian.com)](https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Sending-e-mail-from-Confluence-with-Groovy-ScriptRunner/qaq-p/668039)

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)](https://help.k15t.com/scroll-pdf-exporter/latest/server/export-content-via-rest-api-182787466.html)

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


Answers:
author: Fabian Lim
body: Hi [@Jordan Hauser](/t5/user/viewprofilepage/user-id/4475896)

If you are still having issues, I suggest you contact [adaptivist support](https://scriptrunner.adaptavist.com/latest/jira/getting-help.html) directly if you are paying for this app.

Good luck.


Tags: confluence-server

new-to-confluence

scriptrunner

scroll-pdf

server