質問

I'm trying to convert a HTML file to a PDF by using the Mac terminal.

I found a similar post and I did use the code they provided. But I kept getting nothing. I did not find the output file anywhere when I issued this command:

./soffice --headless --convert-to pdf --outdir /home/user ~/Downloads/*.odt

I'm using Mac OS X 10.8.5.

Can someone show me a terminal command line that I can use to convert HTML to PDF?

役に立ちましたか?

解決 2

Starting 3.6.0.1 , you would need unoconv on the system to converts documents.

Using unoconv with MacOS X

LibreOffice 3.6.0.1 or later is required to use unoconv under MacOS X. This is the first version distributed with an internal python script that works. No version of OpenOffice for MacOS X (3.4 is the current version) works because the necessary internal files are not included inside the application.

他のヒント

I'm trying to convert a HTML file to a PDF by using the Mac terminal.

Ok, here is an alternative way to do convert (X)HTML to PDF on a Mac command line. It does not use LibreOffice at all and should work on all Macs.

This method (ab)uses a filter from the Mac's print subsystem, called xhtmltopdf. This filter is usually not meant to be used by end-users but only by the CUPS printing system.

However, if you know about it, know where to find it and know how to run it, there is no problem with doing so:

  1. The first thing to know is that it is not in any desktop user's $PATH. It is in /usr/libexec/cups/filter/xhtmltopdf.
  2. The second thing to know is that it requires a specific syntax and order of parameters to run, otherwise it won't. Calling it with no parameters at all (or with the wrong number of parameters) it will emit a small usage hint:

    $ /usr/libexec/cups/filter/xhtmltopdf
    
      Usage: xhtmltopdf job-id user title copies options [file]
    

Most of these parameter names show that the tool clearly related to printing. The command requires in total at least 5, or an optional 6th parameter. If only 5 parameters are given, it reads its input from <stdin>, otherwise from the 6ths parameter, a file name. It always emits its output to <stdout>.

The only CLI params which are interesting to us are number 5 (the "options") and the (optional) number 6 (the input file name).

When we run it on the command line, we have to supply 5 dummy or empty parameters first, before we can put the input file's name. We also have to redirect the output to a PDF file.

So, let's try it:

/usr/libexec/cups/filter/xhtmltopdf "" "" "" "" "" my.html > my.pdf

Or, alternatively (this is faster to type and easier to check for completeness, using 5 dummy parameters instead of 5 empty ones):

/usr/libexec/cups/filter/xhtmltopdf 1 2 3 4 5 my.html > my.pdf

While we are at it, we could try to apply some other CUPS print subsystem filters on the output: /usr/libexec/cups/filter/cgpdftopdf looks like one that could be interesting. This additional filter expects the same sort of parameter number and orders, like all CUPS filters.

So this should work:

/usr/libexec/cups/filter/xhtmltopdf   1 2 3 4 5 my.html \
| /usr/libexec/cups/filter/cgpdftopdf 1 2 3 4 ""        \
  > my.pdf

However, piping the output of xhtmltopdf into cgpdftopdf is only interesting if we try to apply some "print options". That is, we need to come up with some settings in parameter no. 5 which achieve something.

Looking up the CUPS command line options on the CUPS web page suggests a few candidates:

-o number-up=4 
-o page-border=double-thick 
-o number-up-layout=tblr 

do look like they could be applied while doing a PDF-to-PDF transformation. Let's try:

/usr/libexec/cups/filter/xhtmltopdfcc 1 2 3 4 5 my.html        \
| /usr/libexec/cups/filter/cgpdftopdf 1 2 3 4 5                \
  "number-up=4 page-border=double-thick number-up-layout=tblr" \
   > my.pdf

Here are two screenshots of results I achieved with this method. Both used as input files two HTML files which were identical, apart from one line: it was the line which referenced a CSS file to be used for rendering the HTML.

As you can see, the xhtmltopdf filter is able to (at least partially) take into account CSS settings when it converts its input to PDF:

First XHTML-to-PDF conversion, CSS stylesheet no. 1 First XHTML-to-PDF conversion, CSS stylesheet no. 2

I just had the same problem, but I found this LibreOffice help post. It seems that headless mode won't work if you've got LibreOffice (the usual GUI version) running too. The fix is to add an -env option, e.g.

libreoffice "-env:UserInstallation=file:///tmp/LibO_Conversion" \
             --headless \
             --invisible \
             --convert-to csv file.xls
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top