2

I am using miktex on windows system. I try to convert .tex file to html and then copy it to word. The challenge comes from .pdf figure

Converting process can be achieved by the previous post converting .pdf figure to .png figure that html can recognize, the import point here is to change gs to mgs for unix system.

However the converted figure has some format problems,

1 the converted figure is doubled size, right blank part is extra due to the convert code. 2 the size of the figure in html is way too large, it holds many times of the page width.

Format problem figure

To control the figure size, i try some code from below, width="\expandafter\the\csname Gin@req@width\endcsname" align=center border=0 onerror="this.src='missing.png'" after using the code, the figure becomes too small.

\Preamble{xhtml}
  \Configure{graphics*}  
         {pdf}  
         {\Needs{"convert '\csname Gin@base\endcsname.pdf'  
                               '\csname Gin@base\endcsname.png'"}%  
          \Picture[pict]{\csname Gin@base\endcsname.png width="\expandafter\the\csname Gin@req@width\endcsname" align=center border=0 onerror="this.src='missing.png'"}%  
          \special{t4ht+@File: \csname Gin@base\endcsname.png}
         }  
\DeclareGraphicsExtensions{.pdf,.png,.jpg,.eps}
\begin{document}
\EndPreamble

Is there some more setting needs to define the pdfconvert and also more setting on the figure style?

1 the blank white margin has been removed by adding a switch in the pdf converting commands as below

\Preamble{xhtml}
\makeatletter
\Configure{PdfConvert}{png}{"mgs -q -dNOPAUSE -dBATCH -sDEVICE=pngalpha -dUseCropBox  -r300 \ifx\Gin@page\@empty\else -dFirstPage="\Gin@page"  -dLastPage="\Gin@page"\fi\space -sOutputFile="\Gin@base-\Gin@page.png" "\Gin@base.pdf" "}
\makeatother
\begin{document}
\EndPreamble

With the switch -dusecropbox, the resolution could be set as high as 300 or higher. However, you will need an extra styl formatting file to define the format.

the style file code are below. basically the two .cfg figure files can not be merged easily to have same figures format as from 2 separate files.

\Preamble{xhtml}

\Configure{@HEAD}{\HCode{<link href="basic.css" rel="stylesheet" type="text/css" />\Hnewline}} 
\DeclareGraphicsRule{.png}{bmp}{.xbb}{}

\ConfigureEnv{table}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="table"\Hnewline>}%
    \bgroup \Configure{float}{\ShowPar}{}{}%
   }
   {\egroup
    \ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar
\par}
{}{}


\ConfigureEnv{figure}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="figure"\Hnewline>}%
    \bgroup \Configure{float}{\ShowPar}{}{}%
   }
   {\egroup
    \ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar
\par}
{}{}

\ConfigureEnv{subfigure}
{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="subfigure">}}
{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{}
\Css{.subfigure{display: inline-block;}}
\Css{.subfigure img{display:block;}}

\Css{div.caption {text-align:left;font-size:83\%;text-indent:0em; margin-left:2em; margin-right:2em; }}
%%%font-variant has 4 values, 1 normal 2 small-caps 3 initial 4 inherit
\Css{div.caption span.id{font-variant: initial; white-space: nowrap; }}
%\Css{div.caption span.id{font-variant: small-caps; white-space: nowrap; }} %%originial 
%%%font-variant has 4 values, 1 normal 2 small-caps 3 initial 4 inherit
\Css{.figure div.caption{text-align: center;}}
\Css{.subfigure div.caption{text-align: center;}}
\Css{div.figure{text-align:center;clear:both;overflow:auto;width:100\%;margin-bottom:1em;}}

%%%%% for table 
%\Css{div.caption{text-align:center;}}
%\Configure{float}{\csname par\endcsname\ShowPar}
%{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="float">}}
%{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar}
%%%%%for table

\begin{document}

\makeatletter
% Various helper functions
% default font size
\newcommand\emwidth{16}
\let\emwidth\f@size
% convert pt to rem
\newcommand\CalcRem[1]{\strip@pt\dimexpr(#1)/\emwidth}


%%%%%%convert pdf to png and set the figure size %%%%%%%%%%
  \Configure{graphics*}  
         {pdf}  
       {\Needs{"convert '\csname Gin@base\endcsname.pdf'  
                               '\csname Gin@base\endcsname.png'"}%  

          \Picture[pict]{\csname Gin@base\endcsname.png width="\expandafter\the\csname Gin@req@width\endcsname" align=center border=0 onerror="this.src='missing.png'"}%  
          \special{t4ht+@File: \csname Gin@base\endcsname.png}
         } 





%%%%%convert pdf to png and another method to set figure size%%%%%%%%
%% you must add the file extension with

%  \Configure{graphics*}  
%         {pdf}  
%         {\Needs{"convert '\csname Gin@base\endcsname.pdf'  
%                               '\csname Gin@base\endcsname.png'"}%  
%          \Picture[pict]{\csname Gin@base\endcsname.png
%           \space style="width:\CalcRem{\Gin@req@width}em;"}%  
%          \special{t4ht+@File: \csname Gin@base\endcsname.png}
%           
%         }  


\Configure{graphics*}
{png}
{% we must add the image to the list of output files
  \special{t4ht+@File: \csname Gin@base\endcsname.png}
  \Picture[pict]{\csname Gin@base\endcsname\PictExt
  \space style="width:\CalcRem{\Gin@req@width}em;"
  }%  
}
\makeatother

\EndPreamble
  • what do you mean by the last sentence? does the style need to be set in the tex4ht configuration file? is it CSS? – michal.h21 Sep 11 '19 at 19:36
  • Hello, michal.h21, the code above can only converting the pdf file to .png file. When the figure is directly inserted html, the display of figure still has some problems. for example, the size is too larger, or some extra line displayed. What i do is to use 2 steps, 2 .cfg files. 1 is to use the above code to only convert .pdf to .png file, in one .cfg file; after all figures are converted i use another .cfg file to define the style of the figure. i attached the style .cfg file above, maybe that is also from you. – shiqiang Sep 11 '19 at 21:27
  • ah I see. I've updated my answer with modified version of your code that requires just one compilation. – michal.h21 Sep 12 '19 at 07:14
  • Thank you, michal.h21. this code works fine! it is just gs needs to be mgs under windows sysem – shiqiang Sep 13 '19 at 01:03
  • yes, of course, I forgot about the mgs stuff :) – michal.h21 Sep 13 '19 at 06:28

1 Answers1

2

The current tex4ht code contains special configuration that holds the shell command for the PDF conversion:

 \Configure{PdfConvert}{output file format}{command}

It is possible to provide a different command for various output formats, like svg or png. The command is executed only if the converted file doesn't exist, thus saving some compilation time.

The following configuration can be used to convert PDF to PNG:

\Preamble{xhtml}
\makeatletter
\Configure{PdfConvert}{png}{"mgs -q -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r120 \ifx\Gin@page\@empty\else -dFirstPage="\Gin@page"  -dLastPage="\Gin@page"\fi\space -sOutputFile="\Gin@base-\Gin@page.png" "\Gin@base.pdf" "}
\makeatother
\begin{document}
\EndPreamble

The important parameter for image dimension is -r120, it is the image resolution. The default resolution is -r300, which is maybe too much.

Edit:

Here is an updated configuration based on the OP's configuration file. It specifies parameters for the PDF conversion directly in the configuration of the PDF inclusion:

\Preamble{xhtml}

\Configure{@HEAD}{\HCode{<link href="basic.css" rel="stylesheet" type="text/css" />\Hnewline}} 
\DeclareGraphicsRule{.png}{bmp}{.xbb}{}

\ConfigureEnv{table}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="table"\Hnewline>}%
    \bgroup \Configure{float}{\ShowPar}{}{}%
   }
   {\egroup
    \ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar
\par}
{}{}


\ConfigureEnv{figure}
   {\ifvmode\IgnorePar\fi\EndP\HCode{<div class="figure"\Hnewline>}%
    \bgroup \Configure{float}{\ShowPar}{}{}%
   }
   {\egroup
    \ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar
\par}
{}{}

\ConfigureEnv{subfigure}
{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="subfigure">}}
{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}}{}{}
\Css{.subfigure{display: inline-block;}}
\Css{.subfigure img{display:block;}}

\Css{div.caption {text-align:left;font-size:83\%;text-indent:0em; margin-left:2em; margin-right:2em; }}
%%%font-variant has 4 values, 1 normal 2 small-caps 3 initial 4 inherit
\Css{div.caption span.id{font-variant: initial; white-space: nowrap; }}
%\Css{div.caption span.id{font-variant: small-caps; white-space: nowrap; }} %%originial 
%%%font-variant has 4 values, 1 normal 2 small-caps 3 initial 4 inherit
\Css{.figure div.caption{text-align: center;}}
\Css{.subfigure div.caption{text-align: center;}}
\Css{div.figure{text-align:center;clear:both;overflow:auto;width:100\%;margin-bottom:1em;}}

%%%%% for table 
%\Css{div.caption{text-align:center;}}
%\Configure{float}{\csname par\endcsname\ShowPar}
%{\ifvmode\IgnorePar\fi\EndP\HCode{<div class="float">}}
%{\ifvmode\IgnorePar\fi\EndP\HCode{</div>}\ShowPar}
%%%%%for table

\begin{document}

\makeatletter
% Various helper functions
% default font size
\newcommand\emwidth{16}
\let\emwidth\f@size
% convert pt to rem
\newcommand\CalcRem[1]{\strip@pt\dimexpr(#1)/\emwidth}


%%%%%%convert pdf to png and set the figure size %%%%%%%%%%
\makeatletter
  \Configure{graphics*}  
         {pdf}  
       {%
   \ifdefined\Gin@page\else\def\Gin@page{1}\fi%
     \edef\svg@filename{\Gin@base-\Gin@page\PictExt}%
     % test for existence of the converted file
     \openin15=\svg@filename\relax
      \ifeof15
      \Needs{"gs -q -dNOPAUSE -dBATCH -dusecropbox -sDEVICE=pngalpha -r120 \ifx\Gin@page\@empty\else -dFirstPage="\Gin@page"  -dLastPage="\Gin@page"\fi\space -sOutputFile="\Gin@base-\Gin@page.png" "\Gin@base.pdf" "}%  
      \fi
      \closein15
          \Picture[pict]{\svg@filename\space width="\expandafter\the\csname Gin@req@width\endcsname" align=center border=0 onerror="this.src='missing.png'"}%  
          \special{t4ht+@File: \svg@filename}
         } 

\makeatother



%%%%%convert pdf to png and another method to set figure size%%%%%%%%
%% you must add the file extension with

%  \Configure{graphics*}  
%         {pdf}  
%         {\Needs{"convert '\csname Gin@base\endcsname.pdf'  
%                               '\csname Gin@base\endcsname.png'"}%  
%          \Picture[pict]{\csname Gin@base\endcsname.png
%           \space style="width:\CalcRem{\Gin@req@width}em;"}%  
%          \special{t4ht+@File: \csname Gin@base\endcsname.png}
%           
%         }  


\Configure{graphics*}
{png}
{% we must add the image to the list of output files
  \special{t4ht+@File: \csname Gin@base\endcsname.png}
  \Picture[pict]{\csname Gin@base\endcsname\PictExt
  \space style="width:\CalcRem{\Gin@req@width}em;"
  }%  
}
\makeatother

\EndPreamble
michal.h21
  • 50,697