Thymeleaf: Template not accessible downloading a PDF online with Spring Boot 2.0.3

1.2k Views Asked by At

I'm downloading a PDF file with a web app, developed with Spring Boot 2.0.3 and using Thymeleaf, from an admin section, protected with Spring Security. Locally it works fine, but online I get this error:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/email/confirmedbooking", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

This is the controller:

@GetMapping("/admin/bookings/booking-pdf")
public void generatePdfBooking(@RequestParam Long idbooking, HttpServletResponse response)
                               throws IOException, Exception{

    bookingService.setBookingService(idbooking);
    Booking booking = bookingService.getBooking();
    Guest guest = bookingService.getGuest();

    String idlanguage;
    if(guest.getIdlanguage() != null){
        idlanguage = guest.getIdlanguage();
    } else {idlanguage = "en";}

    Map<String, Object> map = new HashMap<>();
    map.put("booking", booking);
    map.put("guest", guest);

    byte[] data = pdfGenerator.createPdf("/email/confirmedbooking", map, idlanguage);

    pdfGenerator.streamReport(response, data, "id-" + booking.getIdbooking() + ".pdf");
}

This is an extract of the html page:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <link rel="stylesheet" href="/webjars/bootstrap /css/bootstrap.min.css"/>
    <link rel="stylesheet" href="/css/rentalwebs.css"/>
</head>

<body>
    <table style="width:680px" class="table table-borderless">
        <tbody>
            <tr>
                <td>
                    <h2 th:text="${property.name}"></h2>
                </td>
            </tr>
            <tr>
                <td>
                    <span th:text="#{booking.id}"></span>
                    <span th:text="${booking.idbooking}"></span>
                    ....

As a template generator I'm using org.xhtmlrenderer.pdf.ITextRenderer (flying-saucer-pdf).

I've tested providing different .html files to generate the PDF at pdfGenerator.createPdf("/email/confirmedbooking", map, idlanguage);, but the result is always the same.

1

There are 1 best solutions below

0
On

Following the advice from Daniel Mikusa, I have been able to solve the issue, taking out the first / from /email/confirmedbooking, leaving the code like this:

byte[] data = pdfGenerator.createPdf("email/confirmedbooking", map, idlanguage);

It seems that it didn't recognise the path, probably because of the double slash issue, already explained at this post: Error resolving template with Spring Boot using Thymeleaf packaged in a jar