Skip to content

Commit 60811ac

Browse files
committed
[ci skip] really fix printing
1 parent f768106 commit 60811ac

File tree

4 files changed

+70
-21
lines changed

4 files changed

+70
-21
lines changed

composeApp/src/desktopMain/kotlin/de/l4zs/html2pdfform/backend/converter/HtmlConverter.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package de.l4zs.html2pdfform.backend.converter
22

33
import com.lowagie.text.*
4+
import com.lowagie.text.pdf.PdfAction
45
import com.lowagie.text.pdf.PdfWriter
56
import de.l4zs.html2pdfform.backend.config.ConfigContext
67
import de.l4zs.html2pdfform.backend.data.Context
78
import de.l4zs.html2pdfform.backend.data.field.*
89
import de.l4zs.html2pdfform.backend.extension.*
10+
import de.l4zs.html2pdfform.backend.util.Actions
911
import de.l4zs.html2pdfform.resources.*
1012
import de.l4zs.html2pdfform.util.Logger
1113
import org.jetbrains.compose.resources.getString
@@ -70,6 +72,9 @@ class HtmlConverter(
7072

7173
pdf.open()
7274
pdf.add(Chunk("")) // prevent exception when no content is added
75+
writer.setAdditionalAction(PdfWriter.WILL_PRINT, PdfAction.javaScript(Actions.willPrint, writer))
76+
writer.setAdditionalAction(PdfWriter.DID_PRINT, PdfAction.javaScript(Actions.didPrint, writer))
77+
writer.setPageAction(PdfWriter.PAGE_OPEN, PdfAction.javaScript(Actions.pageOpen, writer))
7378

7479
pdf.setHeaderFooter(config)
7580
writeIntro(pdf, locationHandler, writer)

composeApp/src/desktopMain/kotlin/de/l4zs/html2pdfform/backend/data/field/File.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package de.l4zs.html2pdfform.backend.data.field
22

3-
import com.lowagie.text.pdf.PdfFormField
43
import de.l4zs.html2pdfform.backend.data.Context
54
import org.jsoup.nodes.Element
65

@@ -15,11 +14,7 @@ class File(
1514
element: Element,
1615
context: Context,
1716
id: Int = context.currentElementIndex,
18-
) : Text(element, context, id, FieldType.FILE) {
19-
init {
20-
fieldFlags = fieldFlags or PdfFormField.FF_FILESELECT
21-
}
22-
}
17+
) : Text(element, context, id, FieldType.FILE)
2318

2419
/**
2520
* Creates a file form field. If a label is present, it will be used as

composeApp/src/desktopMain/kotlin/de/l4zs/html2pdfform/backend/data/field/Text.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ open class Text(
8989
)
9090
}
9191

92-
additionalActions[PdfFormField.AA_JS_FORMAT]!!.add(Actions.Placeholder.formatPlaceholder(placeholder ?: ""))
92+
if (placeholder != null) {
93+
additionalActions[PdfFormField.AA_JS_FORMAT]!!.add(Actions.Placeholder.formatPlaceholder(placeholder))
94+
}
9395
}
9496

9597
override fun write() {

composeApp/src/desktopMain/kotlin/de/l4zs/html2pdfform/backend/util/Actions.kt

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,47 @@ package de.l4zs.html2pdfform.backend.util
22

33
/** Actions are JavaScript snippets that can be executed in a PDF form. */
44
object Actions {
5+
val pageOpen =
6+
"""
7+
global.skipValidation = false;
8+
""".trimIndent()
9+
10+
val willPrint =
11+
"""
12+
global.skipValidation = true;
13+
global.passwords = [];
14+
for (var i = 0; i < this.numFields; i++) {
15+
var f = this.getField(this.getNthFieldName(i));
16+
if (f && f.type == "text") {
17+
f.display = display.visible;
18+
if (f.value == "") {
19+
f.value = " ";
20+
if (f.password) {
21+
f.password = false;
22+
global.passwords.push(f.name);
23+
}
24+
}
25+
}
26+
}
27+
""".trimIndent()
28+
29+
val didPrint =
30+
"""
31+
for (var i = 0; i < this.numFields; i++) {
32+
var f = this.getField(this.getNthFieldName(i));
33+
if (f && f.type == "text") {
34+
if (f.value == " ") {
35+
f.value = "";
36+
}
37+
}
38+
}
39+
for (var i = 0; i < global.passwords.length; i++) {
40+
var f = this.getField(global.passwords[i]);
41+
f.password = true;
42+
}
43+
global.skipValidation = false;
44+
""".trimIndent()
45+
546
/** JavaScript snippets for Checkbox fields. */
647
object Checkbox {
748
/**
@@ -33,7 +74,9 @@ object Actions {
3374
*/
3475
fun formatDate(format: String): String =
3576
"""
36-
AFDate_FormatEx("$format");
77+
if (!global.skipValidation) {
78+
AFDate_FormatEx("$format");
79+
}
3780
""".trimIndent()
3881

3982
/**
@@ -44,7 +87,9 @@ object Actions {
4487
*/
4588
fun formatTime(format: String): String =
4689
"""
47-
AFTime_FormatEx("$format");
90+
if (!global.skipValidation) {
91+
AFTime_FormatEx("$format");
92+
}
4893
""".trimIndent()
4994

5095
/**
@@ -55,7 +100,9 @@ object Actions {
55100
*/
56101
fun keystrokeDate(format: String): String =
57102
"""
58-
AFDate_KeystrokeEx("$format");
103+
if (!global.skipValidation) {
104+
AFDate_KeystrokeEx("$format");
105+
}
59106
""".trimIndent()
60107

61108
/**
@@ -66,7 +113,9 @@ object Actions {
66113
*/
67114
fun keystrokeTime(format: String): String =
68115
"""
69-
AFTime_Keystroke("$format");
116+
if (!global.skipValidation) {
117+
AFTime_Keystroke("$format");
118+
}
70119
""".trimIndent()
71120
}
72121

@@ -76,7 +125,7 @@ object Actions {
76125
val keystrokeNumber =
77126
"""
78127
var numberRegex = new RegExp(/^-?[0-9]*$/);
79-
if (!event.willCommit && event.change && !global.isResettingForm) {
128+
if (!event.willCommit && event.change && !global.skipValidation) {
80129
event.rc = numberRegex.test(event.change);
81130
}
82131
""".trimIndent()
@@ -93,7 +142,7 @@ object Actions {
93142
message: String,
94143
): String =
95144
"""
96-
if (event.value && !global.isResettingForm) {
145+
if (event.value && !global.skipValidation) {
97146
var isLess = event.value < $min;
98147
if (isLess) {
99148
app.alert("$message");
@@ -114,7 +163,7 @@ object Actions {
114163
message: String,
115164
): String =
116165
"""
117-
if (event.value && !global.isResettingForm) {
166+
if (event.value && !global.skipValidation) {
118167
var isMore = event.value > $max;
119168
if (isMore) {
120169
app.alert("$message");
@@ -137,7 +186,7 @@ object Actions {
137186
message: String,
138187
): String =
139188
"""
140-
if (event.value && !global.isResettingForm) {
189+
if (event.value && !global.skipValidation) {
141190
var isInvalid = Math.abs(event.value - $base) % $step > 0;
142191
if (isInvalid) {
143192
app.alert("$message");
@@ -159,10 +208,8 @@ object Actions {
159208
"""
160209
if (!event.value) {
161210
event.value = "$placeholder";
162-
event.target.display = display.noPrint;
163211
event.target.textColor = color.ltGray;
164212
} else {
165-
event.target.display = display.visible;
166213
event.target.textColor = color.black;
167214
}
168215
""".trimIndent()
@@ -236,7 +283,7 @@ object Actions {
236283
*/
237284
val buttonDown =
238285
"""
239-
global.isResettingForm = true;
286+
global.skipValidation = true;
240287
""".trimIndent()
241288

242289
/**
@@ -245,7 +292,7 @@ object Actions {
245292
*/
246293
val buttonUp =
247294
"""
248-
global.isResettingForm = false;
295+
global.skipValidation = false;
249296
""".trimIndent()
250297
}
251298

@@ -307,7 +354,7 @@ object Actions {
307354
message: String,
308355
): String =
309356
"""
310-
if (event.value && !global.isResettingForm) {
357+
if (event.value && !global.skipValidation) {
311358
var isLess = event.value.length < $minLength;
312359
if (isLess) {
313360
app.alert("$message");
@@ -331,7 +378,7 @@ object Actions {
331378
): String =
332379
"""
333380
var regex = new RegExp(/$pattern/);
334-
if (event.value && !global.isResettingForm) {
381+
if (event.value && !global.skipValidation) {
335382
var isValid = regex.test(event.value);
336383
if (!isValid) {
337384
app.alert("${message ?: defaultMessage}");

0 commit comments

Comments
 (0)