Trouvez des sneakers Stan Smith, Nike et Veja pour femme à moins de 60€ et à votre taille ! Le tout en moins de 30 secondes. Cliquez ici

Convert an SVG logo to PDF in Java and resize it

PDF invoice with SVG logo that was converted to PDF
A PDF document with a SVG logo that was converted to PDF

In this Java tutorial, we will convert an apple SVG logo to PDF, resize it and place it in the top-left corner of an example PDF invoice. We will use the Apache Batik and iText libraries.

1) Convert the SVG logo to PDF

Add the following dependencies to your POM :

<dependency>
  <groupId>org.apache.xmlgraphics</groupId>
  <artifactId>fop</artifactId>
  <version>2.5</version>
</dependency>
<dependency>
  <groupId>org.apache.xmlgraphics</groupId>
  <artifactId>batik-codec</artifactId>
  <version>1.13</version>
</dependency>

Then the convert logic is simply (replace SVG_PATH and PDF_PATH with your own values) :

A PDF file with path PDF_PATH is generated and it contains the exact same logo as in the file with path SVG_PATH.

2) Resize the PDF logo

In my case, the obtained PDF logo has a size of 300 x 300, while an A4 page has a size of 595 x 842. My PDF logo is currently very big and I want to make it smaller so that it fits nicely in the top-left corner of my final invoice PDF (as shown in the picture above). So we are going to generate a second PDF file, that will be smaller than the current PDF logo, and with same aspect ratio. Let's use the iText library and perform an affine transformation. First add the following dependencies to your POM :

<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>kernel</artifactId>
  <version>7.1.5</version>
</dependency>
<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>layout</artifactId>
  <version>7.1.5</version>
</dependency>

The shrink logic is the following (replace the strings PDF_PATH, PDF_SHRINKED_PATH and the double RATIO with your own values) :

A PDF file with path PDF_SHRINKED_PATH is generated and it contains the same PDF logo but resized. In my case I used RATIO=0.22, and my resulting resized logo now has a size of 66x66 (versus 300x300 originally). We can see the difference in size below :

Original PDF logo left and resized logo right
Original PDF logo left and resized logo right

3) Add the logo to the top-left corner of an A4 page

Let's add the resized logo to the top-left corner of an A4 PDF page (replace LOGO_PATH and INVOICE_PATH with your own values) :

An A4 PDF file with path INVOICE_PATH is generated, and it contains an apple logo in the top-left corner.

Now you have all the tools to automatically build in Java your own professional PDF documents starting from SVG logos. You can then add text and tables for example by using the awesome iText library. If you need help with your code, leave us a reply, we reply within 24 hours.

Leave a Reply

Your email address will not be published. Required fields are marked *

ModeSeeker : comparateur de promotions

Nous recherchons les meilleures promotions disponibles sur internet, pour que vous puissiez trouver facilement des vêtements de marque au meilleur prix : polos Ralph Lauren, pulls Tommy Hilfiger, jeans Levi's... Notre algorithme analyse chaque jour des dizaines de sites d'e-commerce, que nous avons sélectionnés pour leur sérieux et leur fiabilité. Nous sommes indépendants de toute marque ou tout site de vêtements.