Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
Perhaps related: tex.stackexchange.com/questions/43391/… –  topskip Feb 28 '12 at 22:55

2 Answers 2

up vote 11 down vote accepted

Try this configuration file:

\Preamble{xhtml}
  \Configure{graphics*}  
         {pdf}  
         {\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}
         }  
\begin{document}
\EndPreamble

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

Edit:
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).

share|improve this answer
    
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!! –  FEQ Feb 29 '12 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 Feb 29 '12 at 14:06
    
@michal.h21: Are you aware of this related question? –  krlmlr Dec 13 '12 at 13:27
1  
For reference: \DeclareGraphicsExtensions{.pdf} can be added to myxhtml.cfg between \begin{document} and \EndPreamble. –  krlmlr Dec 13 '12 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 Dec 13 '12 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:

\Preamble{xhtml}
  \newcommand{\ConfigureGraphicsDirect}[3]{%
    \Configure{graphics*}
         {#1}
         {\Needs{"#3 \csname Gin@base\endcsname.#1
                               \jobname\arabic{texforhtimagecounter}y.#2"}%
          \Picture[pict]{\jobname\arabic{texforhtimagecounter}y.#2}%
          \stepcounter{texforhtimagecounter}%
         }%
  }
  \ConfigureGraphicsDirect{pdf}{png}{convert -density 300}%
  \ConfigureGraphicsDirect{png}{png}{cp}%
  \ConfigureGraphicsDirect{jpg}{jpg}{cp}%
  \ConfigureGraphicsDirect{jpeg}{jpg}{cp}%
\begin{document}
  \DeclareGraphicsExtensions{.pdf,.png,.jpg,.jpeg}
  \newcounter{texforhtimagecounter}
  \renewcommand{\AsPicture}[1]{%
    \Picture+[]{}#1\EndPicture}
\EndPreamble

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.

share|improve this answer
    
I get the error AsPicture undefined in 'my.cfg'. –  mmj Nov 27 '13 at 14:27
    
@mmj: With what command line? –  krlmlr Nov 27 '13 at 15:30
    
Both with mk4ht oolatex mainfile "my,ooffice" and htlatex mainfile my. –  mmj Nov 27 '13 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 Nov 27 '13 at 21:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.