0

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

sudo /usr/bin/soffice --headless --nologo --nofirststartwizard --norestore --convert-to pdf:calc_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 (can refer this https://stackoverflow.com/a/19947539 ) 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?

ABHI
  • 122
  • 1
  • 9

2 Answers2

1

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 that spreadsheets file should not be read only.

ABHI
  • 122
  • 1
  • 9
  • when i execute at ***step-2*** it gives following error ```javaldx failed!``` ```Warning: failed to read path from javaldx```. – Devang Hingu May 21 '20 at 09:20
1

Adding on to @ABHI 's answer, the correct path of the user's default macro file is at

Linux: ~/.config/libreoffice/4/user/basic/Standard/Module1.xba

Windows: %AppData%\LibreOffice\4\user\basic\Standard\Module1.xba

Isaac
  • 11
  • 1