diff --git a/code/__DEFINES/paper.dm b/code/__DEFINES/paper.dm
index 4eea9e7c2f6..e77086a40e5 100644
--- a/code/__DEFINES/paper.dm
+++ b/code/__DEFINES/paper.dm
@@ -1,5 +1,7 @@
/// Maximimum number of characters that we allow on paper.
#define MAX_PAPER_LENGTH 5000
+/// Maximimum number of characters that we allow in a book, after unfolding and encoding html.
+#define MAX_BOOK_LENGTH (MAX_PAPER_LENGTH * 2)
/// Max number of stamps that can be applied to the paper in tgui.
#define MAX_PAPER_STAMPS 30
/// Max number of stamp overlays that we'll add to a piece of paper's icon.
diff --git a/code/modules/library/book_info.dm b/code/modules/library/book_info.dm
index 8663069644d..e9a4810818e 100644
--- a/code/modules/library/book_info.dm
+++ b/code/modules/library/book_info.dm
@@ -39,15 +39,19 @@
if(trusted)
content = _content
return
- content = trim(html_encode(_content), MAX_PAPER_LENGTH)
+ content = trim(html_encode(trim(_content, MAX_PAPER_LENGTH)), MAX_BOOK_LENGTH)
/datum/book_info/proc/set_content_using_paper(obj/item/paper/paper)
// Just the paper's raw data.
var/raw_content = ""
for(var/datum/paper_input/text_input as anything in paper.raw_text_inputs)
raw_content += text_input.to_raw_html()
+ raw_content += (text_input.raw_text[length(text_input.raw_text)] == "\n") ? "\n" : "\n\n"
- content = trim(html_encode(raw_content), MAX_PAPER_LENGTH)
+ // Paper raw inputs should already be trimmed to maximum paper length,
+ // so we don't trim here again to avoid cutting into the expanded html.
+ // We still trim to a more lenient length to avoid abuse.
+ content = trim(html_encode(raw_content), MAX_BOOK_LENGTH)
/datum/book_info/proc/get_content(default="N/A")
return html_decode(content) || "N/A"
diff --git a/code/modules/paperwork/paper.dm b/code/modules/paperwork/paper.dm
index 71e3c03cb8a..78f591b3ec3 100644
--- a/code/modules/paperwork/paper.dm
+++ b/code/modules/paperwork/paper.dm
@@ -850,10 +850,8 @@
/// Returns the raw contents of the input as html, with **ZERO SANITIZATION**
/datum/paper_input/proc/to_raw_html()
var/final = raw_text
- if(font)
- final = "[final]"
- if(colour)
- final = "[final]"
+ if(font || colour)
+ final = "[final]"
if(bold)
final = "[final]"
return final