Skip to content

Commit c8eaea9

Browse files
authored
[Confluence]: PDF export function no longer working #1158 BUGFIX (#1161)
* To get the export task's current state, use the 'task/progress' endpoint instead of the deprecated 'runningtaskxml.action?taskId' * fix Requests call without timeout issue * log percentage_complete before sleeping * task result url starts with /wiki, remove it
1 parent 7534087 commit c8eaea9

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

atlassian/confluence.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import json
66

77
from requests import HTTPError
8+
import requests
89
from deprecated import deprecated
910
from atlassian import utils
1011
from .errors import ApiError, ApiNotFoundError, ApiPermissionError, ApiValueError, ApiConflictError, ApiNotAcceptable
@@ -2433,7 +2434,11 @@ def get_page_as_pdf(self, page_id):
24332434
url = "spaces/flyingpdf/pdfpageexport.action?pageId={pageId}".format(pageId=page_id)
24342435
if self.api_version == "cloud":
24352436
url = self.get_pdf_download_url_for_confluence_cloud(url)
2436-
2437+
if not url:
2438+
log.error("Failed to get download PDF url.")
2439+
raise ApiNotFoundError("Failed to export page as PDF", reason="Failed to get download PDF url.")
2440+
# To download the PDF file, the request should be with no headers of authentications.
2441+
return requests.get(url, timeout=75).content
24372442
return self.get(url, headers=headers, not_json_response=True)
24382443

24392444
def get_page_as_word(self, page_id):
@@ -2669,45 +2674,40 @@ def get_pdf_download_url_for_confluence_cloud(self, url):
26692674
:param url: URL to initiate PDF export
26702675
:return: Download url for PDF file
26712676
"""
2672-
download_url = None
26732677
try:
2674-
long_running_task = True
2678+
running_task = True
26752679
headers = self.form_token_headers
26762680
log.info("Initiate PDF export from Confluence Cloud")
26772681
response = self.get(url, headers=headers, not_json_response=True)
26782682
response_string = response.decode(encoding="utf-8", errors="strict")
26792683
task_id = response_string.split('name="ajs-taskId" content="')[1].split('">')[0]
2680-
poll_url = "runningtaskxml.action?taskId={0}".format(task_id)
2681-
while long_running_task:
2682-
long_running_task_response = self.get(poll_url, headers=headers, not_json_response=True)
2683-
long_running_task_response_parts = long_running_task_response.decode(
2684-
encoding="utf-8", errors="strict"
2685-
).split("\n")
2686-
percentage_complete = long_running_task_response_parts[6].strip()
2687-
is_successful = long_running_task_response_parts[7].strip()
2688-
is_complete = long_running_task_response_parts[8].strip()
2689-
log.info("Sleep for 5s.")
2690-
time.sleep(5)
2684+
poll_url = "/services/api/v1/task/{0}/progress".format(task_id)
2685+
while running_task:
26912686
log.info("Check if export task has completed.")
2692-
if is_complete == "<isComplete>true</isComplete>":
2693-
if is_successful == "<isSuccessful>true</isSuccessful>":
2694-
log.info(percentage_complete)
2695-
log.info("Downloading content...")
2696-
log.debug("Extract taskId and download PDF.")
2697-
current_status = long_running_task_response_parts[3]
2698-
download_url = current_status.split("href=&quot;/wiki/")[1].split("&quot")[0]
2699-
long_running_task = False
2700-
elif is_successful == "<isSuccessful>false</isSuccessful>":
2687+
progress_response = self.get(poll_url)
2688+
percentage_complete = int(progress_response.get("progress", 0))
2689+
task_state = progress_response.get("state")
2690+
if percentage_complete == 100:
2691+
running_task = False
2692+
log.info(f"Task completed - {task_state}")
2693+
if task_state == "FAILED":
27012694
log.error("PDF conversion not successful.")
27022695
return None
2696+
log.debug("Extract task results to download PDF.")
2697+
task_result_url = progress_response.get("result")
27032698
else:
2704-
log.info(percentage_complete)
2699+
log.info(f"{percentage_complete}% - {task_state}")
2700+
time.sleep(3)
2701+
log.debug("Task successfully done, querying the task result for the download url")
2702+
# task result url starts with /wiki, remove it.
2703+
task_content = self.get(task_result_url[5:], not_json_response=True)
2704+
download_url = task_content.decode(encoding="utf-8", errors="strict")
2705+
log.debug("Successfully got the download url")
2706+
return download_url
27052707
except IndexError as e:
27062708
log.error(e)
27072709
return None
27082710

2709-
return download_url
2710-
27112711
def audit(
27122712
self,
27132713
start_date=None,

0 commit comments

Comments
 (0)