Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ dependencies {
compileOnly 'org.apache.grails:grails-dependencies-starter-web'

api('org.xhtmlrenderer:flying-saucer-pdf:9.9.5')
api("org.apache.pdfbox:pdfbox:3.0.7")
api("org.apache.pdfbox:pdfbox:3.0.8")

testImplementation platform("org.apache.grails:grails-bom:$grailsVersion")
testImplementation "org.apache.grails:grails-testing-support-datamapping"
Expand Down
2 changes: 1 addition & 1 deletion example/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
grailsVersion=7.0.9
grailsVersion=7.0.17
version=0.1
org.gradle.caching=true
org.gradle.daemon=true
Expand Down
2 changes: 1 addition & 1 deletion example/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
6 changes: 6 additions & 0 deletions example/grails-app/views/_dataUrl-css.gsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="data:text/css;base64,Ym9keSB7IGJhY2tncm91bmQtY29sb3I6ICNmZjAwMDA7IG1hcmdpbjogMDsgfQ=="/>
</head>
<body><p>x</p></body>
</html>
2 changes: 1 addition & 1 deletion example/grails-app/views/_encoding-test.gsp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title> Osobowość </title>
<style type="text/css" >
@font-face {
src: url('${asset.link(src: 'arial.ttf')}');
src: url('${asset.assetPath(src: 'arial.ttf')}');
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: cp1250;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package grails.plugins.rendering.image
import grails.plugins.rendering.RenderingServiceSpec

import javax.imageio.ImageIO
import java.awt.Color

abstract class ImageRenderingServiceSpec extends RenderingServiceSpec {

Expand Down Expand Up @@ -71,6 +72,16 @@ abstract class ImageRenderingServiceSpec extends RenderingServiceSpec {
[width: 4, height: 2] | 800 | 400
}

def "data url stylesheet is applied"() {
when:
def pixel = new Color(image(template: '/dataUrl-css', render: [width: 100]).getRGB(5, 5))

then:
pixel.red > 200
pixel.green < 60
pixel.blue < 60
}

/* // Excercises the normal non http response code as well
@Unroll("render #type to http response")
def renderImagesToHttpResponse() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
package grails.plugins.rendering.pdf

import grails.plugins.rendering.RenderingServiceSpec

import org.apache.pdfbox.pdfparser.PDFParser
import org.apache.pdfbox.Loader
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject
import org.apache.pdfbox.text.PDFTextStripper

class PdfRenderingServiceSpec extends RenderingServiceSpec {
Expand All @@ -29,31 +29,66 @@ class PdfRenderingServiceSpec extends RenderingServiceSpec {
pdfRenderingService
}

protected extractTextLines(Map renderArgs) {
extractTextLines(createPdf(renderArgs))
def "pdf contains rendered template text"() {
when:
def lines = extractTextLines(simpleTemplate)

then:
lines*.trim().containsAll(['This is a PDF!', '1'])
}

def "data url image is embedded in pdf"() {
when:
def imageSizes = loadPdf(dataUriTemplate).withCloseable { PDDocument pdf ->
def resources = pdf.getPage(0).resources
resources.XObjectNames
.collect { resources.getXObject(it) }
.findAll { it instanceof PDImageXObject }
.collect { PDImageXObject img -> [img.width, img.height] }
}

then:
imageSizes == [[5, 5]]
}

def "encoding pdf keeps polish characters"() {
when:
def text = extractTextLines(template: '/encoding-test', base: "http://localhost:${serverPort}/rendering").join('\n')

then:
text.contains('łłłłłasdfasdfłłł')
text.contains('Płeć')
}

def "encoding pdf embeds the custom font"() {
when:
def embeddedFontNames = loadPdf( encodingTemplate).withCloseable { PDDocument pdf ->
def resources = pdf.getPage(0).resources
resources.fontNames
.collect { resources.getFont(it) }
.findAll { it.embedded }
*.name
}

then:
embeddedFontNames.any { it.endsWith('ArialUnicodeMS') }
}

protected extractTextLines(byte[] bytes) {
extractTextLines(createPdf(new ByteArrayInputStream(bytes)))
protected Map getEncodingTemplate() {
[template: '/encoding-test', base: "http://localhost:${serverPort}/rendering"]
}

protected extractTextLines(PDDocument pdf) {
def lines = new PDFTextStripper().getText(pdf).readLines()
pdf.close()
lines
protected byte[] renderPdfBytes(Map renderArgs) {
(pdfRenderingService.render(renderArgs) as ByteArrayOutputStream).toByteArray()
}

protected createPdf(Map renderArgs) {
def inStream = new PipedInputStream()
def outStream = new PipedOutputStream(inStream)
pdfRenderingService.render(renderArgs, outStream)
outStream.close()
createPdf(inStream)
protected PDDocument loadPdf(Map renderArgs) {
Loader.loadPDF(renderPdfBytes(renderArgs))
}

protected createPdf(InputStream inputStream) {
def parser = new PDFParser(inputStream)
parser.parse()
parser.getPDDocument()
protected List<String> extractTextLines(Map renderArgs) {
loadPdf(renderArgs).withCloseable { PDDocument pdf ->
new PDFTextStripper().getText(pdf).readLines()
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
projectVersion=7.0.2-SNAPSHOT
grailsVersion=7.0.9
grailsVersion=7.0.17
javaVersion=17
asciidoctorGradlePluginVersion=4.0.5

Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Loading
Loading