When eps files are provided, htlatex from tex4ht automatically converts to png files so that graphic files can be shown in the final html output. However, I use most of the time the pdf graphic file format with pdflatex. Is there any easy way to make htlatex work for pdf graphic files?


2 Answers 2


Try this configuration file:

         {\Needs{"convert '\csname Gin@base\endcsname.pdf'  
                               '\csname Gin@base\endcsname.png'"}%  
          \Picture[pict]{\csname Gin@base\endcsname.png}%  
          \special{t4ht+@File: \csname Gin@base\endcsname.png}

save it as myxhtml.cfg. This configuration tells htlatex for every image file with pdf suffix, run imagemagick's convert command and then insert the image. You must provide correct file extension for every included image in your source file. Compile the file with command

htlatex filename myxhtml

Added \special{t4ht+@File: \csname Gin@base\endcsname.png}, purpose of this line is to store information about used images to the filename.lg file. This information is important when generated files are copied to another directory (make4ht or tex4ebook are using this information, for example).

  • It works very good. One note: I typically do not specify the graphic file's extension in the \includegraphics statement. However, for the myxhtml.cfg, I needed to specify, for example \includegraphics{filename.pdf}. Or, I can declare in the front: \DeclareGraphicsExtensions{.pdf}. Thanks a lot!!
    – Chang
    Commented Feb 29, 2012 at 13:48
  • 1
    htlatex needs to know the filename extension, so you have to specify it either in each \includegraphics or widt \DeclareGraphicsExtensions
    – michal.h21
    Commented Feb 29, 2012 at 14:06
  • @michal.h21: Are you aware of this related question?
    – krlmlr
    Commented Dec 13, 2012 at 13:27
  • 3
    For reference: \DeclareGraphicsExtensions{.pdf} can be added to myxhtml.cfg between \begin{document} and \EndPreamble.
    – krlmlr
    Commented Dec 13, 2012 at 15:24
  • 1
    @user946850 I can't see solution here. maybe by creating script similar to htlatex, where tex4ht command is called twice - first run for image conversion, the second run for packing odt file with -p option to avoid image conversion
    – michal.h21
    Commented Dec 13, 2012 at 18:46

Based on michal.h21's answer I have derived a solution that accepts (almost) all image types also accepted by default pdflatex. This considerably simplifies conversion of documents that are originally created in a pdflatex-based environment, as no conversion to EPS is required anymore.

PDFs are converted to PNG files with a density of 300 dpi. (I couldn't get SVG to work properly with LibreOffice, perhaps Enhanced Metafile or Windows Metafile could work, too, if Microsoft Word is your final target platform.) PNGs and JPEGs are used as they are, for the latter the extension is always .jpg. I have also added support for images in other than the default location by implementing a similar naming scheme to the one used by tex4ht for autogenerated images.

The process requires the following configuration file, let's call it my.cfg:

         {\Needs{"#3 \csname Gin@base\endcsname.#1
  \ConfigureGraphicsDirect{pdf}{png}{convert -density 300}%

For some reason two runs of mk4ht are required to produce a complete .odt file. After the first run, the images are missing. To compile, type:

mk4ht oolatex mainfile "my,ooffice"
mk4ht oolatex mainfile "my,ooffice"

For consistent dimensioning of other \Pictures, it is worthwhile to use a custom tex4ht.env file. I have found that dvips+convert does a better job here than dvipng. For this, copy the tex4ht.env file on your system to your main TeX file's directory and substitute this line

Gdvipng -T tight -x 1400 -D 72 -bg Transparent -pp %%2:%%2 %%1 -o %%3

(starting with Gdvipng line just after G.png) by:

Gdvips -E -D 300 -pp %%2:%%2 %%1 -o %%3.ps
Gconvert -density 300 %%3.ps %%3

If LibreOffice/OpenOffice is your target you need to manually resize each figure to 32% (for 300 dpi pictures). I haven't found an automatic way to do this, a modification of xtpipes might be required here.

Of course, for this to work on Windows you need to substitute cp by copy in the corresponding \ConfigureGraphicsDirect invocations.

The complete code is at GitHub, however you still might have to use your own copy of tex4ht.env unless you run Ubuntu 12.10 (or later) with a default installation or TeXlive.

I have found a very similar .cfg file at http://people.bath.ac.uk/cspehj/maths-access/xht/, but no description whatsoever.

GIF support is left as exercise for the reader.

  • I get the error AsPicture undefined in 'my.cfg'.
    – mmj
    Commented Nov 27, 2013 at 14:27
  • @mmj: With what command line?
    – krlmlr
    Commented Nov 27, 2013 at 15:30
  • Both with mk4ht oolatex mainfile "my,ooffice" and htlatex mainfile my.
    – mmj
    Commented Nov 27, 2013 at 17:01
  • It's been a while. From what I see now, \AsPicture is a user-defined command to add any LaTeX material as a picture. It should work if you define \AsPicture in your document's preamble, or completely omit the definition. The former is necessary so that the document also works when compiling to PDF.
    – krlmlr
    Commented Nov 27, 2013 at 21:25
  • actually, \AsPicture error catches me too.
    – squid
    Commented Nov 17, 2015 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .