Skip to content

Commit

Permalink
Traduciendo la vista PDF a diferentes idiomas (internacionalizacion l…
Browse files Browse the repository at this point in the history
…18N)
  • Loading branch information
IsaiasMorochi committed Sep 14, 2019
1 parent ce1a19b commit 7f07406
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
39 changes: 27 additions & 12 deletions src/main/java/com/view/pdf/FacturaPdfView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@
import com.lowagie.text.pdf.PdfWriter;
import com.models.entity.Factura;
import com.models.entity.ItemFactura;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.view.document.AbstractPdfView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.util.Locale;
import java.util.Map;

/**
Expand All @@ -24,21 +29,30 @@
@Component("factura/ver")
public class FacturaPdfView extends AbstractPdfView {

// 1era forma
@Autowired private MessageSource messageSource;
@Autowired private LocaleResolver localeResolver;


@Override
protected void buildPdfDocument(Map<String, Object> map, Document document, PdfWriter pdfWriter,
HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
HttpServletRequest request, HttpServletResponse response) throws Exception {

Factura factura = (Factura) map.get("factura");

// 1era forma obtener el locale y messageSource
Locale locale = localeResolver.resolveLocale(request);

// 2da forma obtener el locale y messageSource
MessageSourceAccessor mensajes = getMessageSourceAccessor();

// crea una celda
PdfPCell cell = null;

// crea una tabla
PdfPTable tabla1 = new PdfPTable(1);
tabla1.setSpacingAfter(20);
cell = new PdfPCell(new Phrase("Datos del cliente"));
cell = new PdfPCell(new Phrase(messageSource.getMessage("text.factura.ver.datos.cliente", null, locale)));
cell.setBackgroundColor(new Color(184, 218, 255));
cell.setPadding(8f);
tabla1.addCell(cell);
Expand All @@ -48,35 +62,36 @@ protected void buildPdfDocument(Map<String, Object> map, Document document, PdfW

PdfPTable tabla2 = new PdfPTable(1);
tabla2.setSpacingAfter(20);
cell = new PdfPCell(new Phrase("Datos de la Factura"));
cell = new PdfPCell(new Phrase(messageSource.getMessage("text.factura.ver.datos.factura", null, locale)));
cell.setBackgroundColor(new Color(195, 230, 203));
cell.setPadding(8f);
tabla2.addCell(cell);
tabla2.addCell("Folio: " + factura.getId());
tabla2.addCell("Descripcion: " + factura.getDescripcion());
tabla2.addCell("Fecha: " + factura.getCreateAt());
tabla2.addCell(mensajes.getMessage("text.cliente.factura.folio") + ": " + factura.getId());
tabla2.addCell(mensajes.getMessage("text.cliente.factura.descripcion") + ": " + factura.getDescripcion());
tabla2.addCell(mensajes.getMessage("text.cliente.factura.fecha") + ": " + factura.getCreateAt());

document.add(tabla1);
document.add(tabla2);

PdfPTable tabla3 = new PdfPTable(4);
tabla3.setWidths(new float[] {3.5f, 1, 1, 1});
tabla3.addCell("Producto");
tabla3.addCell("Precio");
tabla3.addCell("Cantidad");
tabla3.addCell("Total");
tabla3.addCell(mensajes.getMessage("text.factura.form.item.nombre"));
tabla3.addCell(mensajes.getMessage("text.factura.form.item.precio"));
tabla3.addCell(mensajes.getMessage("text.factura.form.item.cantidad"));
tabla3.addCell(mensajes.getMessage("text.factura.form.item.total"));

for (ItemFactura item: factura.getItems()){
tabla3.addCell(item.getProducto().getNombre());
tabla3.addCell(item.getProducto().getPrecio().toString());

// tabla3.addCell(item.getCantidad().toString()); //otro formato
cell = new PdfPCell(new Phrase((item.getCantidad().toString())));
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
tabla3.addCell(cell);
tabla3.addCell(item.getCantidad().toString());

tabla3.addCell(item.calcularImporte().toString());
}
cell = new PdfPCell(new Phrase("Total: "));
cell = new PdfPCell(new Phrase( mensajes.getMessage("text.factura.form.total")));
cell.setColspan(3); //ocupe 3 columnas
cell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
tabla3.addCell(cell);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/factura/ver.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h4 class="card-title">
<a th:href="@{'/ver/' + ${factura.cliente.id}}"
class="btn btn-light btn-xs">&laquo; volver</a>
<a class="btn btn-danger btn-xs float-right" th:href="@{'/factura/ver/' + ${factura.id}(format=pdf)}">PDF</a>
<a class="btn btn-danger btn-xs float-right" th:target="_blank" th:href="@{'/factura/ver/' + ${factura.id}(format=pdf)}">PDF</a>
</h4>

<ul class="list-group my-2">
Expand Down

0 comments on commit 7f07406

Please sign in to comment.