mirror of
https://git.imnavajas.es/jjimenez/erp-imprimelibros.git
synced 2026-01-13 00:48:49 +00:00
trabajando en el pdf
This commit is contained in:
@ -2,8 +2,10 @@ package com.imprimelibros.erp;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@ConfigurationPropertiesScan(basePackages = "com.imprimelibros.erp")
|
||||
public class ErpApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@ -1,17 +1,14 @@
|
||||
// com.imprimelibros.erp.pdf.PdfModuleConfig.java
|
||||
package com.imprimelibros.erp.pdf;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "imprimelibros.pdf")
|
||||
public class PdfModuleConfig {
|
||||
/**
|
||||
* Mapa: "TYPE:templateId" -> "ruta thymeleaf" (sin extensión).
|
||||
* Ej: "PRESUPUESTO:presupuesto-a4" -> "pdf/presupuesto-a4"
|
||||
*/
|
||||
private Map<String, String> templates;
|
||||
private Map<String, String> templates = new HashMap<>();
|
||||
|
||||
public Map<String, String> getTemplates() { return templates; }
|
||||
public void setTemplates(Map<String, String> templates) { this.templates = templates; }
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.imprimelibros.erp.pdf;
|
||||
|
||||
import com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder;
|
||||
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@ -11,34 +12,33 @@ import java.io.ByteArrayOutputStream;
|
||||
public class PdfRenderer {
|
||||
|
||||
@Value("classpath:/static/")
|
||||
private Resource staticRoot;
|
||||
private org.springframework.core.io.Resource staticRoot;
|
||||
|
||||
public byte[] renderHtmlToPdf(String html) {
|
||||
try (var baos = new ByteArrayOutputStream()) {
|
||||
var builder = new PdfRendererBuilder();
|
||||
|
||||
builder.useFont(() -> getClass().getResourceAsStream("/static/assets/fonts/OpenSans-Regular.ttf"), "Open Sans",
|
||||
400, com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder.FontStyle.NORMAL, true);
|
||||
builder.useFont(() -> getClass().getResourceAsStream("/static/assets/fonts/OpenSans-SemiBold.ttf"), "Open Sans",
|
||||
600, com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder.FontStyle.NORMAL, true);
|
||||
builder.useFont(() -> getClass().getResourceAsStream("/static/assets/fonts/OpenSans-Bold.ttf"), "Open Sans", 700,
|
||||
com.openhtmltopdf.outputdevice.helper.BaseRendererBuilder.FontStyle.NORMAL, true);
|
||||
|
||||
var builder = new com.openhtmltopdf.pdfboxout.PdfRendererBuilder();
|
||||
builder.useFastMode();
|
||||
builder.withHtmlContent(html, baseUrl());
|
||||
|
||||
// 👇 Base URL para que pueda resolver /assets/css/ y /img/
|
||||
builder.withHtmlContent(html, staticRoot.getURL().toString()); // .../target/classes/static/
|
||||
|
||||
|
||||
// (Opcional) Registrar fuentes TTF
|
||||
builder.useFont(() -> getClass().getResourceAsStream("/static/assets/fonts/OpenSans-Regular.ttf"),
|
||||
"Open Sans", 400, BaseRendererBuilder.FontStyle.NORMAL, true);
|
||||
builder.useFont(() -> getClass().getResourceAsStream("/static/assets/fonts/OpenSans-SemiBold.ttf"),
|
||||
"Open Sans", 600, BaseRendererBuilder.FontStyle.NORMAL, true);
|
||||
builder.useFont(() -> getClass().getResourceAsStream("/static/assets/fonts/OpenSans-Bold.ttf"),
|
||||
"Open Sans", 700, BaseRendererBuilder.FontStyle.NORMAL, true);
|
||||
|
||||
builder.toStream(baos);
|
||||
builder.run();
|
||||
|
||||
return baos.toByteArray();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalStateException("Error generando PDF", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String baseUrl() {
|
||||
try {
|
||||
return staticRoot.getURL().toString();
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
// com.imprimelibros.erp.pdf.TemplateRegistry.java
|
||||
package com.imprimelibros.erp.pdf;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -9,10 +8,15 @@ public class TemplateRegistry {
|
||||
|
||||
public TemplateRegistry(PdfModuleConfig config) {
|
||||
this.config = config;
|
||||
System.out.println("PDF templates registrados => " + config.getTemplates());
|
||||
}
|
||||
|
||||
public String resolve(DocumentType type, String templateId) {
|
||||
String key = type.name() + ":" + templateId;
|
||||
String keyAlt = type.name() + "_" + templateId; // compatibilidad con properties
|
||||
if (config.getTemplates() == null) return null;
|
||||
return config.getTemplates().get(type.name() + ":" + templateId);
|
||||
String value = config.getTemplates().get(key);
|
||||
if (value == null) value = config.getTemplates().get(keyAlt);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user