[Tex/LaTex] pdfpages and XeLaTeX: including pdf with uppercase .PDF extension

pdfpagesxetex

here's the problem I'm finding. I have to include some PDF files in a LaTeX. The LaTeX code is generated at runtime, and has to include whatever PDF file it finds in a given directory. These files might have an uppercase .PDF extension. I have no control to prevent this, and cannot rename them. My engine is XeLaTeX. This all happens on a FreeBSD machine with TeXLive 2013.

On my workstation everything works ok if I do not provide the extension of the file to the \includepdf command. It guesses it is a PDF, and the include goes all right. However, if I provide the complete filename with the uppercase .PDF extension, I get a blank page instead of the include, and the logs complain that the file cannot be found.

Here's a minimal example (with the extension on):

\documentclass[11pt,a4paper]{article}
\usepackage{pdfpages}
\usepackage[margin=1.5cm,landscape,includehead,includefoot]{geometry}
\begin{document}
\nonstopmode
\includepdf[pages=-,pagecommand=\clearpage]{compta.PDF}
\end{document}

And here's a log extract:

./Sans-titre.tex:6: Unable to load picture or PDF file 'compta.PDF'.
<to be read again> 
               }
l.6 ...pages=-,pagecommand=\clearpage]{compta.PDF}

The requested image couldn't be read because
the file was not found.

File: compta.PDF Graphic file (type QTm)

This is strange enough already, but ok, I thought, I'll just keep the extension out.
The thing is, when the same .tex is typeset on the FreeBSD server, if I keep the extension out I get the following in the log:

This is XeTeX, Version 3.1415926-2.5-0.9999.3 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode

! Package pdfpages Error: Cannot find file `compta'.

See the pdfpages package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.6 ...pdf[pages=-,pagecommand=\clearpage]{compta}

! Undefined control sequence.
\AM@checklast ...pii \\ \@tempcntb =\AM@pagecount 
                                                  \relax \ifAM@DVIoutput \if...
l.6 ...pdf[pages=-,pagecommand=\clearpage]{compta}

! Missing number, treated as zero.
<to be read again> 
                   \relax 
l.6 ...pdf[pages=-,pagecommand=\clearpage]{compta}

)
Runaway argument?
\expandafter \AM@setphantomdoc \AM@doclist ,\END \ifthenelse {\boolean \ETC.
! File ended while scanning use of \AM@gobble.
<inserted text> 
                \par 
<*> TMP_test.tex

! Emergency stop.
<*> TMP_test.tex

No pages of output.
Transcript written on TMP_test.log.

and if I put the .PDF extension in, I get this:

This is XeTeX, Version 3.1415926-2.5-0.9999.3 (TeX Live 2013)
 restricted \write18 enabled.
entering extended mode
! Unable to load picture or PDF file 'compta.PDF'.
<to be read again> 
                   }
l.6 ...pages=-,pagecommand=\clearpage]{compta.PDF}

<use  "compta.PDF" >
! Unable to load picture or PDF file 'compta.PDF'.
<to be read again> 
                   \GXT@clipend 
l.6 ...pages=-,pagecommand=\clearpage]{compta.PDF}

! Arithmetic overflow.
<recently read> \calc@Acount 

l.6 ...pages=-,pagecommand=\clearpage]{compta.PDF}

! Arithmetic overflow.
<recently read> \calc@Acount 

This is getting me mad, especially when I see that if the exactly same PDF file has a lowercase extension everything goes smooth.
I've tried adding the following (both toghether and separately) to the preamble:

\DeclareGraphicsExtensions{pdf,PDF,png,PNG,jpg,JPG,jpeg,JPEG,txt,TXT}
\DeclareGraphicsRule{.PDF}{pdf}{*}{}

but to no avail (in fact, the only result I could get from the \DeclareGraphicsRule was that instead of the included PDF I got a print of the path of the PDF file, in the middle of the page.

The pdf file I'm trying to include is located at https://dl.dropboxusercontent.com/u/10487109/compta.PDF (however, as I said, it works fine with a lowercase extension, so I don't think the file itself has a problem)

Can anyone help me out? Thanks in advance.
Cheers


Best Answer

Some graphics driver support uppercase extensions (pdftex.def) others do not. xetex.def with version 2014/04/07 v0.99 does not.

The simple form is to add the support for each extension manually, e.g.:

\DeclareGraphicsRule{.PNG}{QTm}{QTm}{#1}
\DeclareGraphicsRule{.JPG}{QTm}{QTm}{#1}
\usepackage{grfext}
\AppendGraphicsExtensions*{.PNG,.JPG}

However this will not work for .PDF due to bugs.

The following example adds the supported extensions of xetex.def in their uppercase variants and patches commands of xetex.def to fix the bugs.

\documentclass{article}
\usepackage{graphicx}
\usepackage{grfext}

% bug in xetex.def
\usepackage{etoolbox}
\makeatletter
\patchcmd\XeTeX@include@QTm{%
  \lowercase{\edef\type@ext{\Gin@ext}}%
}{%
  \edef\type@ext{%
    \noexpand\lowercase{%
      \noexpand\def\noexpand\type@ext{\Gin@ext}%
    }%
  }%
  \type@ext
}{}{}
\let\Ginclude@QTm\XeTeX@include@QTm 
\patchcmd\G@measure@QTm{%
  \lowercase{\edef\type@ext{#2}}%
}{%
  \edef\type@ext{%
    \noexpand\lowercase{%
      \noexpand\def\noexpand\type@ext{#2}%
    }%
  }%
  \type@ext
}{}{}

\@for\@ext:=\Gin@extensions\do{%
  \uppercase\expandafter{%
    \expandafter\def\expandafter\@EXT\expandafter{\@ext}%
  }%
  \@ifundefined{Gin@rule@\@EXT}{%
    \expandafter
    \let\csname Gin@rule@\@EXT\expandafter\endcsname
        \csname Gin@rule@\@ext\endcsname
    % Patch rule to exchange lowercase extension with uppercase
    % (needed for ".PS" and ".EPS"
    \edef\@tempa{%
      \noexpand\patchcmd
      \expandafter\noexpand\csname Gin@rule@\@EXT\endcsname
      {{\@ext}}{{\@EXT}}{}{}%
    }\@tempa
  }{}%
  \expandafter
  \AppendGraphicsExtensions\expandafter*\expandafter{\@EXT}%
}
\makeatother

\begin{document}
  \includegraphics{TIGER}
\end{document}