CLI: convert custom page range to PDF

According to the Documentation, it should be possible to specify a custom page range when using the command line to convert to PDF. I am using Writer: I recognize that the example given in the Documentation is for Draw, but I believe the export filters are meant be interchangeable.

However, When I run soffice.exe --convert-to 'pdf:writer_pdf_Export:{"PageRange":{"type":"string","value":"3-4"}}' $infile --outdir $outdir in Powershell, where $infile is an .fodt file, it exports the entire document to PDF, rather than only the desired pages 3-4. I have tried specifying a number of different page ranges, but the result so far has always been the entire range.

Any ideas what I can do to make this work? Using LibreOffice 24.2.5.2 on Windows 11.

First, let me notice this excerpt from the Starting LibreOffice Software With Parameters Windows-specific help:

"{install}\program\soffice.com" {parameter}
…
Use soffice.exe instead of soffice.com, when you do not need console

Why is this important for the following? Because using soffice.exe, you may not see diagnostic information from the detached process. Read more here.

Now let me show that this is not a LibreOffice issue, but something in PowerShell. It looks like your command line should be correct, reading the documentation:

To make double-quotation marks appear in a string, enclose the entire string in single quotation marks.

The example there shows, that command like like

'As they say, "live and learn."'

produces output like

As they say, "live and learn."

And that’s what we need. However, the command line that you use (just replacing the exe with com) gives this output to the console:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS & 'C:\Program Files\LibreOffice\program\soffice.com' --convert-to 'pdf:writer_pdf_Export:{"PageRange":{"type":"string","value":"3-4"}}' --outdir path\to\ path\to\pages.odt
convert path\to\pages.odt as a Writer document -> path\to\pages.pdf using filter : writer_pdf_Export:{PageRange:{type:string,value:3-4}}

And indeed, the filter options shown there aren’t proper JSON: they need the double quotes. LibreOffice got wrong parameter text from the shell.

Now compare to the output from cmd.exe console, using the escape syntax required there:

"C:\Program Files\LibreOffice\program\soffice.com" --convert-to "pdf:writer_pdf_Export:{\"PageRange\":{\"type\":\"string\",\"value\":\"3-4\"}}" --outdir path\to\ path\to\pages.odt
convert path\to\pages.odt as a Writer document -> path\to\pages.pdf using filter : writer_pdf_Export:{"PageRange":{"type":"string","value":"3-4"}}

The parameter received by LibreOffice is the correct JSON, and it works.

And suddenly, using the same escaping in PowerShell for the double quotes works, too:

PS & 'C:\Program Files\LibreOffice\program\soffice.com' --convert-to 'pdf:writer_pdf_Export:{\"PageRange\":{\"type\":\"string\",\"value\":\"3-4\"}}' --outdir path\to\ path\to\pages.odt
convert path\to\pages.odt as a Writer document -> path\to\pages.pdf using filter : writer_pdf_Export:{"PageRange":{"type":"string","value":"3-4"}}

Maybe that’s something about use of console vs. shell scripts? I don’t know. But it works in console the way I shown.
It might be a version of PowerShell issue, too. Of course, installing the version of PowerShell from Microsoft Store gives me a different story:

PowerShell 7.4.3
PS & 'C:\Program Files\LibreOffice\program\soffice.com' --convert-to 'pdf:writer_pdf_Export:{"PageRange":{"type":"string","value":"3-4"}}' --outdir path\to\ path\to\pages.odt
convert path\to\pages.odt as a Writer document -> path\to\pages.pdf using filter : writer_pdf_Export:{"PageRange":{"type":"string","value":"3-4"}}

But confusingly, this installation leaves me with two versions installed in parallel, and the older version 5 suggested when I type powershell in the start menu… Go figure.

2 Likes

Thanks for your response. I was unaware of the soffice.com binary, but it does indeed work better for this purpose than soffice.exe; it waits until the command finishes before proceeding to the next command in the script. (Using soffice.exe, I had to manually insert pauses.)

I also just noticed that I accidentally linked to the wrong documentation page in my original post - I’ve corrected the link now, and hopefully you can now see that I had copied the single quote syntax directly from the first example given. And as you noted, running the command with single quotes (but with appropriate backslashes) does work, so it appears the backslashes were what was holding me back. Do you know whether these are necessary only on Windows, or on Linux as well?

Also, I’m going to impose on your generosity by asking a followup unrelated to my original question. :slight_smile: You’ll notice my Powershell command is calling several variables (that I’ve defined elsewhere). However when I replace my page range (3-4) with a variable, the command fails:

soffice.com --convert-to 'pdf:writer_pdf_Export:{\"PageRange\":{\"type\":\"string\",\"value\":\"$PageRange\"}}' $infile --outdir $outdir
convert C:\Temp\test.fodt as a Writer document -> C:\Temp\test.pdf using filter : writer_pdf_Export:{"PageRange":{"type":"string","value":"$PageRange"}}
Overwriting: C:\Temp\test.pdf
Error: Please verify input parameters... (SfxBaseModel::impl_store <file:///C:/Temp/test.pdf> failed: 0xc10(Error Area:Io Class:Write Code:16) at C:/cygwin64/home/buildslave/source/libo-core/sfx2/source/doc/sfxbasemodel.cxx:3272 at C:/cygwin64/home/buildslave/source/libo-core/sfx2/source/doc/sfxbasemodel.cxx:1822)

Do you know of any way to define the desired page range elsewhere, and pass it as a variable to the PDF export command?

The quoting is not “on Windows” or “on Linux”, but in specific terminals / shells. As I demonstrated, it’s different even between versions of the same PowerShell :wink: And yes, on most Linux shells, the command line works as shown in help. Anyway, we show what the application should receive; the chosen shell defines its ways how to pass that info.

I am not very familiar with PowerShell, so unfortunately can’t help with that variables problem.

1 Like

Ah okay, I think I got it. Thank you!