Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
trismegisto-services
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mauro Paolo Josue Zuñiga Mallqui
trismegisto-services
Commits
7a71f8e5
Commit
7a71f8e5
authored
Mar 04, 2026
by
Mauro Paolo Josue Zuñiga Mallqui
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[ADD] PDF REST AHORA ACEPTA BACKGROUND
parent
39ad33ec
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
9 deletions
+53
-9
pdfRest.java
.../java/web/multitask/trismegistoservices/rest/pdfRest.java
+8
-1
PDFService.java
...eb/multitask/trismegistoservices/services/PDFService.java
+45
-8
No files found.
src/main/java/web/multitask/trismegistoservices/rest/pdfRest.java
View file @
7a71f8e5
...
@@ -26,6 +26,7 @@ public class pdfRest {
...
@@ -26,6 +26,7 @@ public class pdfRest {
try
{
try
{
String
html
;
String
html
;
String
fileName
;
String
fileName
;
String
background
;
boolean
isBase64
;
boolean
isBase64
;
if
(
json
!=
null
&&
!
json
.
isEmpty
())
{
if
(
json
!=
null
&&
!
json
.
isEmpty
())
{
...
@@ -33,13 +34,19 @@ public class pdfRest {
...
@@ -33,13 +34,19 @@ public class pdfRest {
html
=
jsonBody
.
getString
(
"html"
);
html
=
jsonBody
.
getString
(
"html"
);
fileName
=
jsonBody
.
optString
(
"file_name"
,
"pdf.pdf"
);
fileName
=
jsonBody
.
optString
(
"file_name"
,
"pdf.pdf"
);
isBase64
=
jsonBody
.
optBoolean
(
"base64"
);
isBase64
=
jsonBody
.
optBoolean
(
"base64"
);
background
=
jsonBody
.
has
(
"background"
)
?
jsonBody
.
getString
(
"background"
)
:
null
;
}
else
{
}
else
{
html
=
params
.
get
(
"html"
);
html
=
params
.
get
(
"html"
);
fileName
=
params
.
getOrDefault
(
"file_name"
,
"pdf.pdf"
);
fileName
=
params
.
getOrDefault
(
"file_name"
,
"pdf.pdf"
);
isBase64
=
Boolean
.
parseBoolean
(
params
.
get
(
"base64"
));
isBase64
=
Boolean
.
parseBoolean
(
params
.
get
(
"base64"
));
background
=
params
.
get
(
"background"
);
}
}
byte
[]
bytes
=
pdfService
.
generatePdf
(
html
);
if
(
background
!=
null
&&
!
background
.
startsWith
(
"http"
)
&&
!
background
.
startsWith
(
"data:"
))
{
background
=
"data:image/png;base64,"
+
background
;
}
byte
[]
bytes
=
pdfService
.
generatePdf
(
html
,
background
);
if
(
isBase64
)
{
if
(
isBase64
)
{
String
base64
=
commonUtils
.
byteToBase64
(
bytes
);
String
base64
=
commonUtils
.
byteToBase64
(
bytes
);
...
...
src/main/java/web/multitask/trismegistoservices/services/PDFService.java
View file @
7a71f8e5
package
web
.
multitask
.
trismegistoservices
.
services
;
package
web
.
multitask
.
trismegistoservices
.
services
;
import
com.itextpdf.io.image.ImageData
;
import
com.itextpdf.io.image.ImageDataFactory
;
import
org.springframework.stereotype.Service
;
import
com.itextpdf.html2pdf.ConverterProperties
;
import
com.itextpdf.html2pdf.ConverterProperties
;
import
com.itextpdf.html2pdf.HtmlConverter
;
import
com.itextpdf.html2pdf.HtmlConverter
;
import
com.itextpdf.html2pdf.resolver.font.DefaultFontProvider
;
import
com.itextpdf.html2pdf.resolver.font.DefaultFontProvider
;
import
org.springframework.stereotype.Service
;
import
com.itextpdf.kernel.colors.DeviceRgb
;
import
com.itextpdf.kernel.events.Event
;
import
com.itextpdf.kernel.events.IEventHandler
;
import
com.itextpdf.kernel.events.PdfDocumentEvent
;
import
com.itextpdf.kernel.geom.PageSize
;
import
com.itextpdf.kernel.geom.Rectangle
;
import
com.itextpdf.kernel.pdf.PdfDocument
;
import
com.itextpdf.kernel.pdf.PdfPage
;
import
com.itextpdf.kernel.pdf.PdfWriter
;
import
com.itextpdf.kernel.pdf.canvas.PdfCanvas
;
import
java.io.
File
;
import
java.io.
ByteArrayOutputStream
;
import
java.io.
FileOutputStream
;
import
java.io.
IOException
;
@Service
@Service
public
class
PDFService
{
public
class
PDFService
{
public
byte
[]
generatePdf
(
String
htmlContent
)
throws
Exception
{
public
byte
[]
generatePdf
(
String
htmlContent
,
String
backgroundUrl
)
throws
Exception
{
try
(
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
())
{
PdfWriter
writer
=
new
PdfWriter
(
baos
);
PdfDocument
pdf
=
new
PdfDocument
(
writer
);
pdf
.
setDefaultPageSize
(
PageSize
.
A4
);
// 1. Creamos el estilo CSS para el fondo
// Usamos @page para que se aplique a todas las páginas del PDF
String
backgroundStyle
=
"<style>"
+
"@page {"
+
" margin: 0;"
+
// Opcional: define márgenes si los necesitas
" background-image: url('"
+
backgroundUrl
+
"');"
+
" background-position: center;"
+
" background-repeat: no-repeat;"
+
" background-size: cover;"
+
"}"
+
"body { margin: 1cm; }"
+
// Margen interno para que el texto no toque los bordes
"</style>"
;
// 2. Concatenamos el estilo al final (o al inicio) del contenido
String
finalHtml
=
htmlContent
+
backgroundStyle
;
ConverterProperties
properties
=
new
ConverterProperties
();
ConverterProperties
properties
=
new
ConverterProperties
();
DefaultFontProvider
fontProvider
=
new
DefaultFontProvider
(
false
,
false
,
false
);
DefaultFontProvider
fontProvider
=
new
DefaultFontProvider
(
false
,
false
,
false
);
fontProvider
.
addFont
(
"/fonts/Roboto-Regular.ttf"
);
fontProvider
.
addFont
(
"/fonts/Roboto-Regular.ttf"
);
properties
.
setFontProvider
(
fontProvider
);
properties
.
setFontProvider
(
fontProvider
);
FileOutputStream
outputStream
=
new
FileOutputStream
(
System
.
getProperty
(
"java.io.tmpdir"
)
+
"/pdf.pdf"
);
HtmlConverter
.
convertToPdf
(
htmlContent
,
outputStream
,
properties
);
// 3. Convertimos (Ya no hace falta el EventHandler manual)
File
file
=
new
File
(
System
.
getProperty
(
"java.io.tmpdir"
)
+
"/pdf.pdf"
);
HtmlConverter
.
convertToPdf
(
finalHtml
,
pdf
,
properties
);
return
org
.
apache
.
commons
.
io
.
FileUtils
.
readFileToByteArray
(
file
);
pdf
.
close
();
return
baos
.
toByteArray
();
}
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment