# Does WebWidget's printToPdf work on Mac?

**URL:** <https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544>\
**Category:** Development\
**Created:** [September 7, 2021, 1:37pm UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544 "2021-09-07T13:37:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [September 7, 2021, 1:37pm UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544/1 "2021-09-07T13:37:05Z")

</div>

Hi. I would like to know if this script to create a pdfReport with a webwidget works on Mac (it does work on Windows):

```auto
#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?

@Sam_Horvath

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 7, 2021, 2:03pm UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544/2 "2021-09-07T14:03:35Z")

</div>

> [@mau\_igna\_06](#):
>
> If it doesn’t work on Mac, do you know any workaround to make it work?

With today’s nightly it runs but the webWidget and the pdf are blank.

Changing the last line to this works:

```auto
webWidget.url = "file://" + htmlPath.replace("\\", "/")

```

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [September 7, 2021, 2:13pm UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544/3 "2021-09-07T14:13:10Z")

</div>

Thanks @pieper.

Is there any function I can call to check in what OS Slicer is running on?

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [September 7, 2021, 2:20pm UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544/4 "2021-09-07T14:20:36Z")

</div>

> Is there any function I can call to check in what OS Slicer is running on?

`slicer.app.os`

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 7, 2021, 2:29pm UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544/5 "2021-09-07T14:29:20Z")

</div>

You don’t need to do anything different based on the os - the `file://` should also work on windows.

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [September 7, 2021, 2:49pm UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544/6 "2021-09-07T14:49:57Z")

</div>

In my PC it doesn’t work with that change. Here is the [result](https://gofile.io/d/aUUz0j).

What is the return of this function `slicer.app.os` on mac?

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [September 7, 2021, 2:57pm UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544/7 "2021-09-07T14:57:46Z")

</div>

Looks like `file:///` is the right thing - it works on mac and should also work on windows.

> [@mau\_igna\_06](#):
>
> What is the return of this function `slicer.app.os` on mac?

But if you really need it:

```auto
>>> slicer.app.os
'macosx'

```

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 8, 2021, 3:03am UTC](https://discourse.slicer.org/t/does-webwidgets-printtopdf-work-on-mac/19544/8 "2021-09-08T03:03:49Z")

</div>

There is no need to implement such low-level logic. You can simply use the `QUrl` class to get URL from local file path:

```auto
>>> qt.QUrl.fromLocalFile(r"c:\tmp").toString()
'file:///c:/tmp'

```
