2017-05-27 4 views
1

Ich muss Prozess erstellen, die eine Rechnung PDF-Datei ausgeben wird. Wir konnten keine bessere Lösung finden, als eine HTML-Datei (mit thymeleaf) zu erstellen und sie mit einer Art Helfer (z. B. itextpdf) in PDF-Datei zu formatieren. Aber ich ermutigte einige Probleme, weil ich CSS-Dateien und Bilder nicht einschließen konnte. Gibt es eine bessere Lösung (kostenlos), um damit umzugehen? Oder kannst du mir dabei helfen? Ich werde meinen Java-Code einschließen. Vielen Dank!Rechnung Generation - Thymoleaf, Frühling, Itextpdf

@Service 
public class InvoiceTemplateHelper { 

private final TemplateEngine templateEngine; 
private ServletContext ctx; 
private HttpServletRequest request; 

@Autowired 
public InvoiceTemplateHelper(TemplateEngine templateEngine, ServletContext ctx, HttpServletRequest request) { 
    this.templateEngine = templateEngine; 
    this.ctx = ctx; 
    this.request = request; 
} 

public String getHTMLInvoice() throws InvoiceCreationException { 
    WebContext context = new WebContext(request, null, ctx); 
    context.setVariable("invoiceTitle", "INVOICE/010101"); 

    String body = templateEngine.process("invoice", context); 
    convertToPDF(body); 
    return body; 
} 

public void convertToPDF(String body) throws InvoiceCreationException { 
    try{ 
     Document document = new Document(PageSize.A4); 
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("invoice.pdf")); 
     document.open(); 
     InputStream is = new ByteArrayInputStream(body.getBytes()); 
     XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); 
     document.close(); 
    } catch (IOException e){ 
     //TODO MESSAGE FOR EXCEPTION 
     throw new InvoiceCreationException(); 
    } catch (DocumentException e){ 
     //TODO MESSAGE FOR EXCEPTION 
     throw new InvoiceCreationException(); 
    } 
} 
} 

Und Dummy-HTML-Datei mit Beispiel-Styling und Daten.

.clearfix:after { 
 
    content: ""; 
 
    display: table; 
 
    clear: both; 
 
} 
 

 
a { 
 
    color: #5D6975; 
 
    text-decoration: underline; 
 
} 
 

 
body { 
 
    position: relative; 
 
    width: 21cm; 
 
    height: 29.7cm; 
 
    margin: 0 auto; 
 
    color: #001028; 
 
    background: #FFFFFF; 
 
    font-family: Arial, sans-serif; 
 
    font-size: 12px; 
 
    font-family: Arial; 
 
} 
 

 
header { 
 
    padding: 10px 0; 
 
    margin-bottom: 30px; 
 
} 
 

 
#logo { 
 
    text-align: center; 
 
    margin-bottom: 10px; 
 
} 
 

 
#logo img { 
 
    width: 90px; 
 
} 
 

 
h1 { 
 
    border-top: 1px solid #5D6975; 
 
    border-bottom: 1px solid #5D6975; 
 
    color: #5D6975; 
 
    font-size: 2.4em; 
 
    line-height: 1.4em; 
 
    font-weight: normal; 
 
    text-align: center; 
 
    margin: 0 0 20px 0; 
 
    background: url(../images/dimension.png); 
 
} 
 

 
#project { 
 
    float: left; 
 
} 
 

 
#project span { 
 
    color: #5D6975; 
 
    text-align: right; 
 
    width: 52px; 
 
    margin-right: 10px; 
 
    display: inline-block; 
 
    font-size: 0.8em; 
 
} 
 

 
#company { 
 
    float: right; 
 
    text-align: right; 
 
} 
 

 
#project div, 
 
#company div { 
 
    white-space: nowrap;   
 
} 
 

 
table { 
 
    width: 100%; 
 
    border-collapse: collapse; 
 
    border-spacing: 0; 
 
    margin-bottom: 20px; 
 
} 
 

 
table tr:nth-child(2n-1) td { 
 
    background: #F5F5F5; 
 
} 
 

 
table th, 
 
table td { 
 
    text-align: center; 
 
} 
 

 
table th { 
 
    padding: 5px 20px; 
 
    color: #5D6975; 
 
    border-bottom: 1px solid #C1CED9; 
 
    white-space: nowrap;   
 
    font-weight: normal; 
 
} 
 

 
table .service, 
 
table .desc { 
 
    text-align: left; 
 
} 
 

 
table td { 
 
    padding: 20px; 
 
    text-align: right; 
 
} 
 

 
table td.service, 
 
table td.desc { 
 
    vertical-align: top; 
 
} 
 

 
table td.unit, 
 
table td.qty, 
 
table td.total { 
 
    font-size: 1.2em; 
 
} 
 

 
table td.grand { 
 
    border-top: 1px solid #5D6975;; 
 
} 
 

 
#notices .notice { 
 
    color: #5D6975; 
 
    font-size: 1.2em; 
 
} 
 

 
footer { 
 
    color: #5D6975; 
 
    width: 100%; 
 
    height: 30px; 
 
    position: absolute; 
 
    bottom: 0; 
 
    border-top: 1px solid #C1CED9; 
 
    padding: 8px 0; 
 
    text-align: center; 
 
}
<!DOCTYPE html> 
 
<html xmlns:th="http://www.thymeleaf.org"> 
 
<head> 
 
    <meta charset="utf-8"/> 
 
    <title th:remove="all">Taxi invoice</title> 
 
    <link rel="stylesheet" th:href="@{css/style.css}" media="all" /> 
 
</head> 
 
<body> 
 
<header class="clearfix"> 
 
    <div id="logo"> 
 
     <img th:src="@{images/logo.png}"/> 
 
    </div> 
 
    <h1 th:text="${invoiceTitle}">Invoice</h1> 
 
    <div id="company" class="clearfix"> 
 
     <div><p th:text="#{company.name}"></p></div> 
 
     <div><p th:text="#{company.address}"></p></div> 
 
     <div><p th:text="#{comapny.phoneNumber}"></p></div> 
 
     <div> 
 
      <a th:mail="'mailto:'+#{company.mail}"> 
 
       <p th:text="#{company.name}"></p> 
 
      </a> 
 
     </div> 
 
    </div> 
 
    <div id="project"> 
 
     <div><span>PROJECT</span> Website development</div> 
 
     <div><span>CLIENT</span> John Doe</div> 
 
     <div><span>ADDRESS</span> 796 Silver Harbour, TX 79273, US</div> 
 
     <div><span>EMAIL</span> <a th:href="'mailto:'+#{company.mail}">[email protected]</a></div> 
 
     <div><span>DATE</span> August 17, 2015</div> 
 
     <div><span>DUE DATE</span> September 17, 2015</div> 
 
    </div> 
 
</header> 
 
<main> 
 
    <table> 
 
     <thead> 
 
     <tr> 
 
      <th class="service">SERVICE</th> 
 
      <th class="desc">DESCRIPTION</th> 
 
      <th>PRICE</th> 
 
      <th>QTY</th> 
 
      <th>TOTAL</th> 
 
     </tr> 
 
     </thead> 
 
     <tbody> 
 
     <tr> 
 
      <td class="service">Design</td> 
 
      <td class="desc">Creating a recognizable design solution based on the company's existing visual identity</td> 
 
      <td class="unit">$40.00</td> 
 
      <td class="qty">26</td> 
 
      <td class="total">$1,040.00</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="service">Development</td> 
 
      <td class="desc">Developing a Content Management System-based Website</td> 
 
      <td class="unit">$40.00</td> 
 
      <td class="qty">80</td> 
 
      <td class="total">$3,200.00</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="service">SEO</td> 
 
      <td class="desc">Optimize the site for search engines (SEO)</td> 
 
      <td class="unit">$40.00</td> 
 
      <td class="qty">20</td> 
 
      <td class="total">$800.00</td> 
 
     </tr> 
 
     <tr> 
 
      <td class="service">Training</td> 
 
      <td class="desc">Initial training sessions for staff responsible for uploading web content</td> 
 
      <td class="unit">$40.00</td> 
 
      <td class="qty">4</td> 
 
      <td class="total">$160.00</td> 
 
     </tr> 
 
     <tr> 
 
      <td colspan="4">SUBTOTAL</td> 
 
      <td class="total">$5,200.00</td> 
 
     </tr> 
 
     <tr> 
 
      <td colspan="4">TAX 25%</td> 
 
      <td class="total">$1,300.00</td> 
 
     </tr> 
 
     <tr> 
 
      <td colspan="4" class="grand total">GRAND TOTAL</td> 
 
      <td class="grand total">$6,500.00</td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
    <div id="notices"> 
 
     <div>NOTICE:</div> 
 
     <div class="notice">A finance charge of 1.5% will be made on unpaid balances after 30 days.</div> 
 
    </div> 
 
</main> 
 
<footer> 
 
    Invoice was created on a computer and is valid without the signature and seal. 
 
</footer> 
 
</body> 
 
</html>

+0

Gibt es einen bestimmten Grund, dass Sie HTML verwenden und es mit iText in PDF konvertieren, anstatt sie direkt mit iText-Methoden zu erstellen? – mkl

+0

Erstens ist HTML mit Thymoleaf viel einfacher, Hexendaten und Stil zu füllen. Aber wenn ich das nicht in angemessener Zeit tun kann. Ich werde einfach Iitext verwenden. – Unlucky

Antwort

1

Werfen Sie einen Blick auf pdfHtml. Es ist das neueste iText Add-on. Es unterstützt HTML5 und CSS 3. Es gibt einige Beispiele bereits, so dass Sie in der Lage sein sollten, einen schnellen Einstieg in die Konvertierung von HTML in ein PDF-Dokument zu bekommen.