Hi. I would like to know if this script to create a pdfReport with a webwidget works on Mac (it does work on Windows):
#get/create directories/paths
import os
desktopPath = os.path.join(os.path.expanduser("~"), "Desktop")
reportFolderPath = os.path.join(desktopPath, "ReportFolder")
if not os.path.exists(reportFolderPath):
os.makedirs(reportFolderPath)
reportPath = os.path.join(
reportFolderPath, "PDFReport.pdf"
)
import tempfile
tempFolderPath = tempfile.mkdtemp()
pdfReportPicturePath = os.path.join(
tempFolderPath, "pdfReportPicture.png"
)
#load CT
import SampleData
sampleDataLogic = SampleData.SampleDataLogic()
ct = sampleDataLogic.downloadCTChest()
#take report picture
import ScreenCapture
redSliceNode = slicer.mrmlScene.GetNodeByID("vtkMRMLSliceNodeRed")
screenCaptureLogic = ScreenCapture.ScreenCaptureLogic()
view = screenCaptureLogic.viewFromNode(redSliceNode)
screenCaptureLogic.captureImageFromView(view, pdfReportPicturePath)
#create html code
_html = f"""
<html>
<body>
<h1>Test PDF Report</h1>
<img src="{pdfReportPicturePath}" alt="" border=3 width=300></img>
<br>
</body>
</html>
"""
#create html file
htmlPath = os.path.join(tempFolderPath, "pdfReport.html")
file = qt.QFile(htmlPath)
file.open(qt.QIODevice.WriteOnly | qt.QIODevice.Text)
file.write(_html)
file.close()
#page layout for the pdf
pageLayout = qt.QPageLayout(
qt.QPageSize(qt.QPageSize.A4),
qt.QPageLayout.Portrait,
qt.QMarginsF(15, 15, 0, 15),
qt.QPageLayout.Millimeter,
)
#create webwidget, load html page, print pdf and open it
webWidget = slicer.qSlicerWebWidget()
def onWebWidgetLoadFinished(result):
webWidget.loadFinished.disconnect(onWebWidgetLoadFinished)
webWidget.pdfPrintingFinished.connect(onWebWidgetPDFPrintingFinished)
webWidget.printToPdf(reportPath, pageLayout)
def onWebWidgetPDFPrintingFinished(filePath, result):
webWidget.pdfPrintingFinished.disconnect(
onWebWidgetPDFPrintingFinished
)
#
from shutil import rmtree
#
rmtree(tempFolderPath, ignore_errors=True)
#
# Open in PDF viewer
print("Starting '" + reportPath + "' ...")
# slash/backlash replacements because of active directory
import subprocess
#
subprocess.Popen([reportPath], shell=True)
webWidget.loadFinished.connect(onWebWidgetLoadFinished)
webWidget.setUrl(htmlPath.replace("\\", "/"))
If it doesn’t work on Mac, do you know any workaround to make it work?