1

I have search but unable to find anything suitable for converting text along with logo image to another image or PDF. It would be great if both image and Pdf options are there, Just like Banking website.

 model.addAttribute("success_message", "Your Reference number is " + referenceNo + ".\nFund Transfer of Amount " + cl.Money(xxDTO.getAmount())
+ " has been made successfully against " + xxDTO.getAccount()
+ " on " + xxDTO.getTime()
+ " from " + xxDTO.getSendAccountNumber()
+ " for " + session.getAttribute("purposeText"));

This is my success message, When funds are transfered, The success messages show on a page inside a div. But I want to add two options there to download as Image or PDF.

How Can I achieve that in Java 11 (Springboot)

I will have to add one static logo at the top and then above text at bottom, and Text format can be changed etc. For testing purpose, I have created a simple HTML file and added a table inside it. It is a static File. I have addded a button and on button click, I am trying to download PDF, Below is code

@requestMapping("/pdf")
    public ResponseEntity<?> getPDF(HttpServletRequest request, HttpServletResponse response) throws IOException {


        /* Create HTML using Thymeleaf template Engine */
        // testingpdf is html view
        String orderHtml = templateEngine.process("testingpdf");

        /* Setup Source and target I/O streams */

        ByteArrayOutputStream target = new ByteArrayOutputStream();

        /*Setup converter properties. */
        ConverterProperties converterProperties = new ConverterProperties();
        converterProperties.setBaseUri("http://localhost:8080");

        /* Call convert method */
        HtmlConverter.convertToPdf(orderHtml, target, converterProperties);

        /* extract output as bytes */
        byte[] bytes = target.toByteArray();


        /* Send the response as downloadable PDF */

        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_PDF)
                .body(bytes);
    }

POM FILE

<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.1.12</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>3.0.1</version>
        </dependency>

The above is already in my pom.xml but these import are not working.

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
Asfandyar Khan
  • 1,677
  • 15
  • 34

1 Answers1

1

You can use iText library for creating and manipulating PDF, you can find documentation in this link about itext.

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.2</version>
</dependency>

There other way to do that by :

  • Converting a div to PDF using jsPDF, the question was already asked here

From @ilyes you can use jsPDF as:

var doc = new jsPDF();
var specialElementHandlers = {
    '#editor': function (element, renderer) {
        return true;
    }
};

$('#cmd').click(function () {
    doc.fromHTML($('#divtoBepdf').html(), 15, 15, {
        'width': 170,
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');
});
  • Converting a div to image(Canva), take a look at this question, this one, and this one.

There other way to do that ...

Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35