What is the best way to set up a daily export of Confluence page to a pdf using scriptrunner jobs?
Asked by Aaron Andrade:

As a new Admin for JIRA/Confluence, I am trying to figure out the path of least resistance to create a scriptrunner job to do a daily export of a Confluence page to a pdf to be saved in a shared file. Any and all help is greatly appreciated.


Answers:
author: Helmy Ibrahim _Adaptavist_
body: Hi Justin,

You can call Confluence's [export to PDF](https://confluence.atlassian.com/conf59/export-content-to-word-pdf-html-and-xml-792498686.html#ExportContenttoWord,PDF,HTMLandXML-ExportsinglepagestoWordorPDF) **URL** to get the PDF file. The request URL should be something like:

```
<Confluence_URL>/spaces/flyingpdf/pdfpageexport.action?pageId=<Page_Id>
```

Then, you can probably use the Apache Common IO FileUtils's [copyURLToFile](https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#copyURLToFile(java.net.URL,%20java.io.File)) to download the file and save it to your destination folder - See [example](https://www.tutorialkart.com/java/java-download-file-from-url/)

Ultimately, your script in Job could be something like:

```
import org.apache.commons.io.FileUtils

// Replace <Confluence_URL>
// Replace <Page_Id>
def requestUrl = "<Confluence_URL>/spaces/flyingpdf/pdfpageexport.action?pageId=<Page_Id>"

// Update destinationPath with your shared directory and file name
def destinationPath = "PATH/FILENAME.pdf"

try {
    URL url = new URL(requestUrl)
    File destinationFile = new File(destinationPath)

    FileUtils.copyURLToFile(url, destinationFile)
} catch (MalformedURLException e) {
    // Handle
} catch (IOException e) {
    // Handle
}
```

I hope this helps!

Cheers,  
Helmy

comments: author: Aaron Andrade
body: Helmy,

Thank you for the response! I am building and trying it today. I hope that all goes smooth if not I'll be back ;).

Thank you,

Justin


author: Helmy Ibrahim _Adaptavist_
body: Hi Justin,

This [post](https://stackoverflow.com/questions/3059383/file-path-windows-format-to-java-format) on StackOverflow should help!

Cheers,  
Helmy


author: Helmy Ibrahim _Adaptavist_
body: Ah you edited your reponse.

Hope everything goes well :)


author: Aaron Andrade
body: I did because when I copied the path it used forward slash instead of backslash. I changed them and the error is gone. Now I am just trying to actually get the pdf to the file.


author: Aaron Andrade
body: Helmy,

So it runs with no errors, but I'm not actually pulling anything. The results come back as null.

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/162496i840F4CEBB3CE35FA/image-size/large?v=v2&px=999 "image.png")


author: Helmy Ibrahim _Adaptavist_
body: Hi Justin,

Is there a **daily.pdf** file created in the target directory at all?


author: Aaron Andrade
body: No there is not. I for some reason thought that was naming the file. I will get that out of there.

\*\*UPDATE\*\* Still did not work unfortunately.


author: Helmy Ibrahim _Adaptavist_
body: Hi Justin,

Can you print the following:

```
def currentDir = System.getProperty("user.dir")
log.warn(currentDir)
```

Cheers  
Helmy


author: Aaron Andrade
body: ![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/162499iA49D85E8BCCC2C0C/image-size/large?v=v2&px=999 "image.png")


author: Jessie Wang_ScriptRunner_The Adaptavist Group
body: Hi all, this script is now available in our script library for ScriptRunner for Confluence Server/DC (tested by our engineers).

Feel free to copy or customise it as you wish.

<https://library.adaptavist.com/entity/export-page-as-pdf-job>


author: Helmy Ibrahim _Adaptavist_
body: My original script above did not handle any authentication. Please use the following script instead: <https://www.mysamplecode.com/2019/04/java-download-image-from-url-authentication.html>


author: Helmy Ibrahim _Adaptavist_
body: Hi Justin,

You don't have to replace the string 'user.dir'. It is literally it:

```
System.getProperty("user.dir")
```

Cheers,  
Helmy

comments: author: Aaron Andrade
body: sorry about that.![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/162500i2DA16514BCDD80DC/image-size/large?v=v2&px=999 "image.png")


author: Helmy Ibrahim _Adaptavist_
body: Can you check if you actually have permission to create a file in that directory?

You can run this script in the **Script Console** to test:

```
def testFile = new File("C:/Users/jmele/Desktop/Export_Test/test.txt")

log.warn testFile.getAbsolutePath()
```


author: Aaron Andrade
body: ![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/162505i986C47EDB5CB49A7/image-size/large?v=v2&px=999 "image.png")


author: Helmy Ibrahim _Adaptavist_
body: Can you copy the script from the Job and run in Script Console? Also, make sure to specify the filename.

```
def destinationPath = "C:/Users/jmele/Desktop/Export_Test/testFile.pdf"
```

Then, check if the file actually created.


author: Aaron Andrade
body: I did the above mentioned and it did not write to the folder. It is an empty folder still.


author: Helmy Ibrahim _Adaptavist_
body: Sorry I missed a line, try:

```
def testFile = new File("C:/Users/jmele/Desktop/Export_Test/test.txt")
testFile.createNewFile()

log.warn testFile.getAbsolutePath()
```


author: Aaron Andrade
body: ![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/162516i99B2ED50CD526A0F/image-size/large?v=v2&px=999 "image.png")![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/162517i8DA56BE294D75F9D/image-size/large?v=v2&px=999 "image.png")


Tags: confluence-server

export

job

pdf

scriptrunner

server