diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp
index ad964593..22023926 100644
--- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp
+++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp
@@ -397,6 +397,16 @@ void XMLCALL ChapterHtmlSlimParser::startElement(void* userData, const XML_Char*
LOG_DBG("EHP", "Display size: %dx%d (scale %.2f)", displayWidth, displayHeight, scale);
}
+ // Flush any pending text block so it appears before the image
+ if (self->partWordBufferIndex > 0) {
+ self->flushPartWordBuffer();
+ }
+ if (self->currentTextBlock && !self->currentTextBlock->isEmpty()) {
+ self->makePages();
+ self->currentTextBlock.reset();
+ self->wordsExtractedInBlock = 0;
+ }
+
// Create page for image - only break if image won't fit remaining space
if (self->currentPage && !self->currentPage->elements.empty() &&
(self->currentPageNextY + displayHeight > self->viewportHeight)) {