# Outputting whole pages as .png

I need to output some .pngs of the whole page (in fact, I need the left and the right page sticked, I am currently doing this by imagemagick). I am aware that this can be done with the standalone class, but I need to do this with whole books in the memoir class (like this one).

I could do this by getting the pdf and then using some pdf utils but I was wondering if there were more efficient ways to do this (as I'll do this thousands of times). Closest I get to my ideal result is using dvipng, using the -T bbox option, which sadly leaves out some margins. Any other options that work directly with the .tex file or the .dvi output?

For an specific case, having as a source file this:

\documentclass[smalldemyvopaper,11pt]{memoir}
\usepackage{lipsum}

\begin{document}
\lipsum[1-5]
\end{document}


And then using the command dvipng -T 5.675in,8.5in example.dvi shows a really different image than what was expected. The 5.675in,8.5in part was taken from the memoir class doc table 1.3. Notice how the left part is cut too.

• dvipng should work if you have told it the paper size, if you have a small complete example where it fails, edit the question to add an example document so people can test. Dec 3, 2019 at 17:18
• oh you're right, however you can use he -O "offset" option to put the margins back, eg dvipng -T5.675in,8.5in -O1in,1in cc383 Dec 3, 2019 at 19:37
• Thanks! How did you find the appropriate values for the offset though, or are they just eyeballed? As there will be other stock sizes I'm going to use. Also, is the cc383 the name of your .tex file? Couldn't find any info on the dvipng --help about that. Dec 3, 2019 at 20:08
• I just got the -Ooption from dvipng --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 Dec 3, 2019 at 20:20
• I use Adobe Acrobat (the "cheap" version) for this. It does the whole document with one "save as". Dec 3, 2019 at 21:05

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:

• I knew it could be done this way but was looking to do it with .dvi for effiency but the dvi approach is harder than expected so I'll stick with this, thanks! Also by 'sticking' the pages I meant the equivalent to convert leftside.png rightside.png +append sticked.png Dec 3, 2019 at 20:36
• Just for future reference, turns out this is extremly slow, at least compared to dvipng. For example, a png-ing a 64 page pdf takes around 13.8s, while png-ing the same document in .dvi takes 1.1s. Dec 3, 2019 at 23:48
• @Shiranai: let me add something about the use of dvipng, since it is faster, like you said. Dec 4, 2019 at 14:25
• One final remark about dvipng; on my desktop, pdftoppm defaults to 150 dpi for the output, while dvipng defaults to 100 dpi. This doesn't really effect the speed differential. But if you want higher resolution output, make sure to specify the -D option first. On my desktop at least putting -D first and -D last in the list of options give two different images. Dec 4, 2019 at 14:53