Macro Calc: View pdf after export (ViewPDFAfterExport)?

How do I export a spreadsheet to pdf and perform the ‘View pdf after export’ action using macro in python?
I am using the code below but the ViewPDFAfterExport filter is not working. Tested on linux and windows.


I need to perform the same action as the command indicated in the image

Could it be that in python it is not the same filter name as in basic?
See in the example the ‘Watermark’ filter works normally.

import uno
from pathlib import Path
from com.sun.star.beans import PropertyValue
from com.sun.star.io import IOException

def dict_to_property(values: dict, uno_any: bool=False):
    ps = tuple([PropertyValue(Name=n, Value=v) for n, v in values.items()])
    if uno_any:
        ps = uno.Any('[]com.sun.star.beans.PropertyValue', ps)
    return ps


def export_pdf_args():
    doc = XSCRIPTCONTEXT.getDocument()

    dict_filter_options = {
        'ViewPDFAfterExport' : True,
        'Watermark' : 'test',
    }

    filter_data = dict_to_property(dict_filter_options, True)
    
    dict_media_descriptor = {
        'FilterName': 'calc_pdf_Export',
        'FilterData': filter_data,
    }

    sys_output_pdf = Path(Path.home(), f'''{doc.Title}.pdf''')
    lo_output_pdf = uno.systemPathToFileUrl(str(sys_output_pdf))

    args = dict_to_property(dict_media_descriptor)
    try:
        doc.storeToURL(lo_output_pdf, args)
    except IOException as e:
        print(f'''Location is unknown\n\n{e}''')

The above code exports the pdf, but does not display the pdf automatically

1 Like

My PC doesn’t work either.

You can do, then:

If you’re on Linux:

subprocess.Popen(['xdg-open', sys_output_pdf])

If you’re on Windows:

os.startfile(sys_output_pdf)
1 Like

The ViewPDFAfterExport filter option is only considered when performing the export using GUI. UNO export ignores it by design. If you pass your descriptor to .uno:ExportToPDF UNO command, using a dispatcher, then it should work; when you use storeToURL API, it is expected to be ignored, and you can use Python methods to launch the stored file in a viewer explicitly after export (as @elmau described).

Thank you for the explanation.
I understand your justification

I did the same test using Basic and storeToURL
The ViewPDFAfterExport doesn’t work either

Sub ExportPdfArgs
    Dim oDoc As Object
    Dim aFilterData(1) As new com.sun.star.beans.PropertyValue
    Dim aMediaDescriptor(1) As new com.sun.star.beans.PropertyValue
    Dim sURL As String
    Dim sLoOutputPdf As String

    GlobalScope.BasicLibraries.LoadLibrary("Tools")

    oDoc = ThisComponent

    aFilterData(0).Name = "ViewPDFAfterExport"
    aFilterData(0).Value = true
    aFilterData(1).Name = "Watermark"
    aFilterData(1).Value = "test"

    aMediaDescriptor(0).Name = "FilterName"
    aMediaDescriptor(0).Value = "calc_pdf_Export"
    aMediaDescriptor(1).Name = "FilterData"
    aMediaDescriptor(1).Value = aFilterData

    sURL = oDoc.getURL()
    sLoOutputPdf = GetFileNameWithoutExtension(sURL) & ".pdf"

    oDoc.storeToURL(sLoOutputPdf, aMediaDescriptor)
End Sub