5

I try to convert a latex pdf to png and the background should be transparent.

So I use pdftopng from XpdfReader for windows to convert: (bat-File)

pdflatex Other.tex
pdfcrop Other.pdf
pdftopng -r 300 Other-crop.pdf Other
del Other-crop.pdf

The source is:

\documentclass[border=10pt,tikz]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw [very thick,fill=green!20!white]
  (-4, 4) circle (2.0cm) node {\textbf{A}};
\end{tikzpicture}

\end{document}

from the example how to remove the background for png picture

with the result:

enter image description here

Is there a way to convert the pdf to a png with a transparent background using pdftopng?

2 Answers 2

4

With ImageMagick instead of pdftopng it's simple:

convert -density 300 Other-crop.pdf Other.png

enter image description here

3
  • Do you have an idea what I have to do, that for example the "A" in the circle get transparent too? Is there a way in tikz/latex to print something that interpreted as transparent in png?
    – Mar Tin
    Feb 22, 2019 at 15:24
  • @MarTin If you change the colour of A to white (or any other colour that is not the same as the line around the circle), you can use the -transparent white option
    – user181777
    Feb 22, 2019 at 15:35
  • thanks you for your advice to imagemagick, I really like the project and I edit my first answer with the imagemagick solution. Have good evening.
    – Mar Tin
    Feb 22, 2019 at 18:03
3

Solution for pdftopng: (transparent background only)

pdflatex Other.tex
pdfcrop Other.pdf
pdftopng -r 300 -alpha Other-crop.pdf Other
del Other-crop.pdf

Prepare for imagemagick:

  1. download imagemagick
  2. download ghostscript

Solution for imagemagick:

define a transparent color in latex:

\definecolor{ColorTrans}{RGB}{0, 255, 0}

fill elements with it that should be transparent (background excluded)

\begin{document} %DOCUMENT BEGIN
\begin{tikzpicture}
\draw [very thick,fill=green!20!white,text=white,font=\Huge] (-5, 4) circle (1.0cm) node {\textbf{A}};
\draw [very thick,fill=green!20!white,text=ColorTrans,font=\Huge] (-3, 4) circle (1.0cm) node {\textbf{A}};
\end{tikzpicture}
\end{document}

run bat-file and define the transparent color:

pdflatex Other.tex
pdfcrop Other.pdf
magick convert -density 300 -transparent "rgb(0,255,0)" Other-crop.pdf Other.png 
del Other-crop.pdf

result:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .