答案1
背景:我有一个文件夹树,里面有 4,500 张 *.jpg 图片。我一直在研究一种将所有图片自动打印成单个 pdf 文件的方法。到目前为止,如果您将此程序放在包含图片的目录中,然后对其进行编译,您将获得一个包含该文件夹中所有图片的 pdf。我已将其更改为打印 *.png 文件,并验证它在我的系统上运行正常。Win 8.1。
我也没有删除任何正在进行的讨论,其中包括我如何解决某些问题的参考。未解决的问题:文件名中的多余句点必须手动删除;一次只能处理一个文件夹,并且真的希望能够循环遍历整个树。
\documentclass[12pt,letterpaper,landscape]{book}
\usepackage[margin=.5in]{geometry}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage[space]{grffile} % Suggested by http://tex.stackexchange.com/questions/8422/how-to-include-graphics-with-spaces-in-their-path
%% Suggested by http://tex.stackexchange.com/questions/69142/include-figure-from-macro-with-underscore-in-filename
%% Suggested by http://tex.stackexchange.com/questions/172996/adjustbox-and-includegraphics-page-selection
\newcommand{\PlotFrameB}[1]{%
\begin{center}\includegraphics[min size={\textwidth}{0.9\textheight},max size={\textwidth}{0.9\textheight}]{#1}\par \textbf{{#1}}\end{center}\endgroup}
\def\PlotFrame{\begingroup
\catcode`\_=12
\PlotFrameB}
%%%%%%%
%%Suggested by http://tex.stackexchange.com/questions/53458/inserting-figures-using-loops/53645#53645
\edef\subdir{"Sub Dir/"}
\graphicspath{{\subdir}}
%% Removed from path above
\immediate\write18{cmd /c dir /b \subdir\space *.png > imagelist.txt}
%%%%%%%%
\begin{document}
\textbf{Known and Solved Problems}
\begin{itemize}
\item Doing all of the below in Win 8.1, MikTeX 2.9
\item \textbf{SOLVED} Using graphics files with spaces in the file names.
\begin{verbatim}
...
\usepackage[space]{grffile}
...
\end{verbatim}
\item \textbf{SOLVED} Using graphics files with underscores in the file names.
\begin{verbatim}
...
\newcommand{\PlotFrameB}[1]{%
\begin{center}%
\includegraphics[min size={\textwidth}{0.9\textheight},%
max size={\textwidth}{0.9\textheight}]{#1}\par \textbf{{#1}}%
\end{center}\endgroup}
\def\PlotFrame{\begingroup
\catcode`\_=12
\PlotFrameB}
...
\end{verbatim}
\item \textbf{SOLVED} How to maximize the size of the picture when either height or width could be the limiting factor\par
\begin{verbatim}
...
\usepackage{graphicx}
\usepackage[export]{adjustbox}
...
... \includegraphics[min size={\textwidth}{0.9\textheight},max size={\textwidth}{0.9\textheight}]{#1} ...
...
\end{verbatim}
\item \textbf{SOLVED} How to loop through all the pictures with *.jpg extension in the folder
\begin{verbatim}
...
\makeatletter
\newread\reader
\openin\reader=imagelist.txt\relax
\begingroup
\endlinechar=-1\relax
\loop
\readline\reader to \data
\unless\ifeof\reader
\filename@parse{\data}
\PlotFrame{"\filename@base"}\endgraf\newpage
\repeat
\endgroup
\closein\reader
\makeatother
...
\end{verbatim}
\item How to set the graphics paths so only one copy of this program is needed to go thru all the photos in tree and build about 4500 pages at once.
\end{itemize}
\newpage
\makeatletter
\newread\reader
\openin\reader=imagelist.txt\relax
\begingroup
\endlinechar=-1\relax
\loop
\readline\reader to \data
\unless\ifeof\reader
\filename@parse{\data}
\PlotFrame{"\filename@base"}\endgraf\newpage
\repeat
\endgroup
\closein\reader
\makeatother
\end{document}