private File doCreatePdfForPage(User user, AbstractPage page, String contextPath) throws ImportExportException {
exportPermissionChecker.checkAuthorization(user, page);
Document xhtml;
if (page instanceof Page) {
ContentTree tree = newContentTree();
tree.addRootNode(new ContentNode((Page) page));
xhtml = intermediateHtmlBuilder.buildHtml(tree, page.getSpace(), LinkRenderingDetails.anchors(), DecorationPolicy.none());
} else if (page instanceof BlogPost) {
xhtml = intermediateHtmlBuilder.buildHtml((BlogPost) page);
} else {
throw new IllegalArgumentException("Only pages and blog post are supported");
}
String spaceKey = getSpaceKeyForExportFileName(page.getSpaceKey());
File pdf;
try {
String filename = spaceKey + "-" + ((page.getTitle() == null) ? page.getId() : page.getTitle().replaceAll("\\s", ""));
if (!GeneralUtil.isSafeTitleForFilesystem(filename)) {
filename = spaceKey + "-" + page.getId();
}
pdf = flyingPdfDocumentConverter.convertXhtmlToPdf(filename, xhtml, contextPath);
} catch (ImportExportException e) {
if (e.getCause() instanceof FileNotFoundException) {
String filename = spaceKey + "-" + page.getId();
pdf = flyingPdfDocumentConverter.convertXhtmlToPdf(filename, xhtml, contextPath);
} else {
throw e;
}
}
return pdf;
}