When i export a PDF from Writer Macro I do net get the trailer dictionary nor xref tables

I create a pdf file using this basic code:

sub ExportToPDF(documentIn as object, URL as String)
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = documentIn.CurrentController.Frame
dispatcher = createUnoService(“com.sun.star.frame.DispatchHelper”)

rem ----------------------------------------------------------------------
'dispatcher.executeDispatch(document, “.uno:SaveAs”, “”, 0, Array())

rem ----------------------------------------------------------------------
dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = “URL”
args2(0).Value = URL
args2(1).Name = “FilterName”
args2(1).Value = “writer_pdf_Export”

'dispatcher.executeDispatch(document, “.uno:ExportDirectlyToPDF”, “”, 0, args2())
documentIn.storeToURL(URL, storeArgs())

end sub

The resultant pdf opens in Writer but not in my Windows 11 Chrome browser.

In ubuntu I run this and get the listed results:

root@minion2:/var/www/gas# pdfinfo working/test_out.pdf
Syntax Warning: May not be a PDF file (continuing anyway)
Syntax Error (12): Illegal character ‘}’
Syntax Error: Couldn’t find trailer dictionary
Syntax Error: Couldn’t find trailer dictionary
Syntax Error: Couldn’t read xref table
root@minion2:/var/www/gas#

And what specifically does run that basic code? You may be sure, that different versions of LibreOffice (at least I hope that you use LibreOffice and not e.g. AOO, don’t you?) have different bugs :slight_smile:

Hi mikekaganski, Thanks for the quick reply. I’m running version 24.2.7.2 (x86_64). Ubuntu package version: 4:24.2.7-0ubuntu0.24.04.2.

Did you check the PDF in a plain text editor? Is it really a PDF? Your last answer (that you posted as a “solution” :slight_smile:) hints that it could be not the case. Could you attach it here?

Thanks again mikekaganski. I have altered my sub and gotten a valid pdf:

sub ExportDirectToPDF(documentIn as object, URL as String)
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService(“com.sun.star.frame.DispatchHelper”)

rem ----------------------------------------------------------------------
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = “URL”
args1(0).Value = URL
args1(1).Name = “FilterName”
args1(1).Value = “writer_pdf_Export”

dispatcher.executeDispatch(document, “.uno:ExportDirectToPDF”, “”, 0, args1())

end sub

The difference is in the line “document = ThisComponent.CurrentController.Frame”. This alteration gives me a pdf if i put a break point before the sub and then step through the commands. If I run it without a break point I get an error (object not set) at that line.

Do you know how I get the right object based on my document object so that it PDFs my document?

I have completed my macro except for this seeming bug. Does anyone know why it will run interactively after a breakpoint is added but not without one. It doesn’t matter where the breakpoint is. Can anyone generate a pdf from a Writer document using Basic?

Use of dispatcher is not the optimal way to generate PDFs anyway.

Great I’ll take any alternative.

storeToURL is your API. E.g.: Export as PDF via API ignores all arguments - #5 by sokol92

Thanks mikekaganski. You solved it and thanks to ChatGPT, here’s my final code:

Dim oProperties(0) As New com.sun.star.beans.PropertyValue
oProperties(0).Name = “FilterName”
oProperties(0).Value = “writer_pdf_Export”
’ Save the document as PDF
MasterDoc.storeToURL(“file:///” & document_save_path, oProperties)

I had left off the oProperties.

Thanks again for your help, you star.

Please never do this to convert a filesystem path to URL. Use ConvertToURL for that, to avoid invalid URLs, e.g. where space is not percent-encoded, or backslashes (on Windows) are kept as is.

(post deleted by author)