If you have access to it, I would use the pdftoppm
util which is part of the poppler
utilities for manipulating pdf files.
(I am not sure what you meant by having the left and right page "sticked".)
Taking your example file (which I name as testpng.tex
) I can run
pdflatex testpng.tex
which generates the pages as PDFs. Then I run
pdftoppm -png testpng.pdf tpn
(the -png
switch asks to output in png format, and the tpn
argument sets the prefix of the output png files)
This command generates three png files (since you started with three pages in the PDF output) named tpn-1.png
, tpn-2.png
, and tpn-3.png
. The second page, for example, looks like

which I think has the correct margins. You can read the man page to see what else it can do.
An alternative is to use the dvipng
tool, which, as the OP noted, runs faster. The problem the OP observed is that after specifying the paper size, the margins appear incorrectly.
This is because how dvipng
outputs based on the -T
parameter:
- If
-T bbox
is specified, then dvipng outputs an image that is the smallest rectangle which includes
- the dvi origin (which by default is the point 1 inch both horizontally and vertically from the top left corner of the page)
- and all the ink on the page.
- If
-T tight
is specified, then dvipng outputs an image that is the smallest rectangle which includes all the ink on the page.
- If
-T <width>,<height>
outputs an image that is a rectangle that is of the specified dimension, whose contents is the rectangle of that size measured relative to the dvi origin (which, remember, is the point 1 inch from both the top and left edge of the paper).
To illustrate this, consider the following TeX source
\documentclass{book}
\usepackage{geometry}
\geometry{paperwidth=5in, paperheight=7in, inner=0.4in, outer=2in, top=1.2in, bottom=2in}
\usepackage{lipsum}
\begin{document}
\lipsum[1-5]
\end{document}
This has heavily unbalanced margins. If you use dvipng -T5in,7in
, what you see for the first two pages are:
Page 1:

Page 2:

You see that part of the text on page 1 is truncated, that is because the inner margin is set to be 0.4in which is less than 1 in from the edge. You can also measure if you wish to find that in both cases the remaining top margin is exactly 0.2in, which means both page numbers now disappear.
To get the full page as intended, all you need to do is to shift the dvi origin by exactly 1 inch both horizontally and vertically, so that it now lines up with the top left corner of the page. Note that no guesswork or eyeballing is required. Here is the outputs for the same two pages using dvipng -T5in,7in -O1in,1in
; note that the heavily asymmetrical margins are respected and unlike what David suggested, no fiddling around with the document's actual margins is required to set the offset parameter.
Page 1:

Page 2:

-O
"offset" option to put the margins back, egdvipng -T5.675in,8.5in -O1in,1in cc383
cc383
the name of your .tex file? Couldn't find any info on thedvipng --help
about that.-O
option fromdvipng --help
then guessed an inch margin to look about right, i could have been more exact but your real doc may have different margins anyway, so i left it at that