# htlatex with pdfpages package inserts random images

I am attempting to generate an HTML output via

htlatex <filename>


but when the pgfpages package is included I am getting some random images before and after the content. The resulting HTML viewed in Safari looks like:

I looked at pdfpages.sty and see that there is an \AtBeginDocument but don't see how that inserts these random images before the content. Furthermore, did not find a \AtEndDocument so can't understand how anything is happening after the content is processed.

## Code:

\documentclass{article}
\usepackage{pdfpages}

\begin{document}
% \includepdf{foo.pdf}% Cover page

Some content following cover page.
\end{document}

• do you use pdfpages just to include some one page pdf file, or do you want to use some more advanced options? – michal.h21 Oct 6 '14 at 6:45
• @michal.h21: I am using pdfpages to include the cover page of the book. But, as the above MWE demonstrates just including the package (without using it) results in the random images. – Peter Grill Oct 6 '14 at 6:53

pdfpages is obviously one of packages which causes tex4ht to fail just with loading. some other example of such package is fontspec. only solution is to prevent usage of such packages when tex4ht is loaded and to provide alternative definitions for macros used in the document.

To simplify this process, I created simple package, called alternative4ht.sty:

\ProvidesPackage{alternative4ht}

\newcommand{\altusepackage}[2][]{%
\ifx\HCode\relax%
\usepackage[#1]{#2}%
\else%
\IfFileExists{#2-alt4ht.sty}{%
\usepackage[#1]{#2-alt4ht}%
}{\typeout{alternative4ht: no patch file for #2}}
\fi%
}
\endinput


one command is defined, \altusepackage, which loads package with normal LaTeX, but with tex4ht it loads alternative definitions or it does nothing if such alternative doesn't exist.

You can modify your source:

\documentclass{article}
\usepackage{alternative4ht}
\altusepackage{pdfpages}

\begin{document}
%\includepdf{foo.pdf}% Cover page

Some content following cover page.
\end{document}


you can provide definition for \includepdf in file pdfpages-alt4ht.sty:

\ProvidesPackage{pdfpages-alt4ht}
\RequirePackage{graphicx}

\newcommand\includepdf[2][]{%
\includegraphics{#2}
}

\endinput


and you will need to add support for pdf image inclusion with custom config file, peter.cfg:

\Preamble{xhtml}
\makeatletter
\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}
}
\makeatother
\begin{document}
\EndPreamble


now you can compile the document with:

htlatex <filename> peter


and the resulting html is:

<?xml version="1.0" encoding="iso-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd-->
<html xmlns="http://www.w3.org/1999/xhtml"
>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)" />
<meta name="originator" content="TeX4ht (http://www.cse.ohio-state.edu/~gurari/TeX4ht/)" />
<!-- html,xhtml -->
<meta name="src" content="sample.tex" />
<meta name="date" content="2014-10-06 09:55:00" />
<link rel="stylesheet" type="text/css" href="sample.css" />