We will be migrating from Ask to Discourse on the first week of August, read the details here

Ask Your Question
0

Libreoffice .xls to .pdf conversion breaks single page content into multiple pages on Ubuntu 18.04

asked 2020-02-07 08:10:35 +0200

ABHI gravatar image

updated 2020-09-01 13:00:57 +0200

Alex Kemp gravatar image

I am facing issue during conversion of .xls to .pdf through libreoffice command line mentioned below.

/usr/bin/soffice --headless --nologo --nofirststartwizard --norestore --convert-to pdf:writer_web_pdf_Export --outdir '/home/user/Downloads/' '/home/user/Downloads/file.xlsx'

Its converted successfully, however it breaks .xls single content page into multiple pages in pdf. I want to convert xls single content page to pdf single page through page scaling. We can do that manually through Menu Formatting -> Page ->Sheet ->Scale Scaling mode. But I don't want user interference during conversion of Xls to Pdf!

I have gone through many Articles but not found the proper solution as I want it through command line. Is there any option in command line syntax to scale it on the fly?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2020-03-03 06:48:20 +0200

ABHI gravatar image

updated 2020-03-03 13:22:58 +0200

We can achieve this through applying macros on that specific XLSX on run time and after than we can execute PDF conversion command. The steps I followed to implement this solution mentioned below.

Step 1 :- Store a macro on server.

in my case I have stored here (/opt/libreoffice/presets/basic/Standard/Module1.xba). Content of Module1.xba is

<?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
    <!--
    * This file is part of the LibreOffice project.
    *
    * This Source Code Form is subject to the terms of the Mozilla Public
    * License, v. 2.0. If a copy of the MPL was not distributed with this
    * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    *
    * This file incorporates work covered by the following license notice:
    *
    *   Licensed to the Apache Software Foundation (ASF) under one or more
    *   contributor license agreements. See the NOTICE file distributed
    *   with this work for additional information regarding copyright
    *   ownership. The ASF licenses this file to you under the Apache
    *   License, Version 2.0 (the "License"); you may not use this file
    *   except in compliance with the License. You may obtain a copy of
    *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
    -->
    <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">REM  *****  BASIC  *****
    Sub FitToPage
      Dim document As Object, pageStyles As Object
      document   = ThisComponent
      pageStyles = document.StyleFamilies.getByName("PageStyles")
      For i = 0 To document.Sheets.Count - 1
       Dim sheet As Object, style As Object
       sheet = document.Sheets(i)
       style = pageStyles.getByName(sheet.PageStyle)
       style.ScaleToPagesX = 1
      Next
      On Error Resume Next
      document.storeSelf(Array())
      document.close(true)
    End Sub
    Sub Main

    End Sub
    </script:module>

Step 2:- Apply the above macro on xls file through below command.

$cmd = 'export HOME=/var/www &&  /opt/libreoffice/program/soffice --headless --nologo --nofirststartwizard --norestore  /home/usr/demo.xls  macro:///Standard.Module1.FitToPage';

Step 3 :- Generate Pdf after applying macro through command line.

$cmd = 'export HOME=/var/www && /opt/libreoffice/program/soffice --headless --nologo --nofirststartwizard --norestore  --convert-to pdf:writer_web_pdf_Export  --outdir  /home/usr/  /home/usr/demo.xls';

Note :- The only catch in this implementation is the spreadsheets file should not be read only.

edit flag offensive delete link more
0

answered 2020-02-07 08:44:37 +0200

updated 2020-02-07 08:50:49 +0200

First of all, you used a wrong export filter name in your command line. As explained here, in this case you (not) needed to use calc_pdf_Export, which is used by default anyway when you export a document handled by Calc and use pdf output specification.

But that will not change anything. Until version 6.4, there was no way to do what you need. In 6.4, a Full-Sheet Previews feature (supposedly to be renamed soon?) was implemented. It has no command-line API (yet? just like any other PDF export setting available in the PDF export dialog); but you may note that whatever settings you set in the dialog in the UI, are saved for following use when you export to PDF directly, and also when you export using command line.

So a simple way would be:

  1. Install version 6.4;
  2. Export a spreadsheet to PDF interactively (in UI mode), and in the export dialog, set the mentioned option checked;
  3. Use the command line you used.

Of course, that would make your export dependent on the export setting defined in the UI, which might be not ideal in case when you use LibreOffice both for batch processing, and interactively on the same box. In this case, you might want the settings used for batch processing to be independent from interactive mode. To do that, you may use -env command line parameter to specify a separate user profile directory; so starting LibreOffice interactively with the said parameter once to initialize the profile and make necessary filter settings, and then adding that switch to batch processing command line, would allow you to have the necessary customizations independent of your UI usage.

edit flag offensive delete link more

Comments

@Mike Thanks for the detailed information, however "To do that, you may use -env command line parameter to specify a separate user profile directory; so starting LibreOffice interactively with the said parameter once to initialize the profile and make necessary filter settings, and then adding that switch to batch processing command line, would allow you to have the necessary customizations independent of your UI usage." still I didn't understand how to do this through -env command!! Can you please show me step by step to set scaling parameter through -env , it would be more helpful to me.

ABHI gravatar imageABHI ( 2020-02-07 13:36:40 +0200 )edit

Did you try what I write before that -env part, to see what I'm talking about?

Mike Kaganski gravatar imageMike Kaganski ( 2020-02-07 13:52:23 +0200 )edit

@Mike As above you have mentioned that after saving in dialog box configuration you can export through UI and command prompt but I don't want to save anything on dialog box, I want to do all the configuration part also through command line only. As you have already mentioned, there is no command line API, So it will not solve my purpose.

Later you have mentioned "-env" configuration option. What I have understood we will set same set of configuration which we have set through dialog box on server side directly in diff env and then when we will call it through command line those configuration will automatically get apply. If I am correct then I want to understand whatever configuration we want to save on server side (For exp:- Full sheet preview) what would be the syntax to configure this configuration and in which file do I need ...(more)

ABHI gravatar imageABHI ( 2020-02-10 06:03:55 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-02-07 08:10:35 +0200

Seen: 2,564 times

Last updated: Mar 03 '20