How to Convert a LaTeX PDF to PNG?

Your pictures look nice. What are you using to export them from latex to a blog image?  

- Austins -

Hi guys!

Many thanks for your messages and the encouraging comments!

I've decided to write a tutorial about compiling \LaTeX documents into a PNG images based on your suggestion.  You may find this useful in the following cases: 

  • Sharing on social media,
  • Blogging,
  • PowerPoints if you are not Fan of beamer, 
  • Coffee mugs with your TikZ images, and much more.

Use standalone class...

Most of the time, I use Overleaf (Online \LaTeX editor) to draw my illustrations and I choose Standalone as a documentclass. This class allows us to get an output file that only contains the picture with no extra (or specified) white margins. Here is an example of an equation with 0.2cm white margin: 

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\node at (0,0) {\Huge \bf $e^{i\pi}=-1$};
\end{tikzpicture}
\end{document}

and here is a screenshot of what you will get with Overleaf:

How to export PNG file in LaTeX 

The idea is to create a configuration file in Overleaf project that contains just a single line: 

END { system('convert -density 600 output.pdf myImage.png'); } 

The file is named latexmkrc as shown below:

After a successful compilation of the configuration file, we get a png file in the tab logs and output files with the same name specified in the line code (myImage.png). Let's go deeper!

The line command calls a Perl function system(...) which excutes a program called convert. The latter does the conversion of the pdf file (output.pdf) at 600 dpi (specified using -density 600) to a PNG file with the chosen name myImage.png. In this post, we will highlight some useful options and the reader may refer to ImageMagick's online help for a complete version.

How to resize the generated image file

In order to change the size of the genrated image file, we can add -resize as follows:

END { system('convert -density 600 -resize 20% output.pdf myImage.png'); } 

This will produce an image with a size 20% of the original image.  

25% of the original image.

Original image.

How to get a transparent background

By default, the generated image has a transparent background. In order to change its colour (for example to yellow), we add -background yellow -flatten to the line code: 

END { system('convert -density 600 -background yellow -flatten output.pdf myImage.png'); } 

Yellow background

Cyan background

How to export a JPEG image from LaTeX

This can be done by changing the name of the image file from myImage.png to myImage.jpeg. That's all!

END { system('convert -density 600 -background yellow -flatten output.pdf myImage.jpeg'); } 

Convert a specific page of a document to an Image 

Until now, we have considered standalone class to export TikZ illustrations to an image instead of a PDF file. Suppose we have a document with many pages and we  would like to convert one page to an image. In this case, we modify output.pdf to output[numberOfPage].pdf. Here is an example of exporting page 2 of the PDF document to a PNG image with a name page2:

END { system('convert -density 600 -background white -flatten output[2].pdf page2.png'); } 

Design your own coffee mug

At this level, we assume you know how to convert your TikZ illustration to PNG with a transparent background. In this section, we will provide an easy method to take your TikZ illustrations to another level 😊, printing them on your daily coffee mug. To this end, we will use Printful.com, a simple and easy to use plateform for Print-on-demand.

At this level, we assume you know how to convert your TikZ illustration to PNG with a transparent background. In this section, we will provide an easy method to take your TikZ illustrations to another level 😊, printing them on your daily coffee mug. 


White Glossy Mug

White Ceramic Mug with Color Inside

Matte Black Magic Mug

Glossy Black Magic Mug

Step 2 - Upload and Customize your TikZ Illustration

Once you choose your prefered mug, you can specify its size (11oz or 15oz). Then you can upload your illustration: 

Once you upload your PNG image, then you have the possibility to put it on one side of the mug, both sides or create a pattern. You can also change the outside color of the mug from "Print area background color". You also have the possibility to add text and clipart.

Important: use the mockup view to get an idea about how your coffee mug looks.

Shipping speed differs from a country to another. For example, for the UK it takes from 2 to 5 days to get your order. For the US, it takes 3 to 4 days. Shipping prices are shown in the table below. For more details, check shipping speeds and pricing.

Disclosure: This page contains affiliate links. If you click through and purchase an item, I may earn a commission (at no additional cost to you) to keep the website alive. Thanks! 


Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu