I am currently working on incorporating a sidebar to the left side of a generated PDF whenever there is text enclosed within a span with the class name sidebar
. One major issue I am encountering is that when the text spans across multiple lines, calculating the height or coordinates of the span in JavaScript within wkhtmltopdf results in inaccurate values. This makes it difficult for me to accurately size or position the generated sidebar element. I am open to any suggestions or ideas that could help me resolve this issue.
Some of the approaches I have experimented with include:
- Creating the sidebar as a
:before
pseudo-element within the span (as shown below). When setting the span's position torelative
and the:before
pseudo-element toabsolute
with properties likeheight: 100%
andleft: 0
, it does create a bar that matches the height of the span but it appears inside the span and I struggle to find the correct left offset value to reposition the bar. In wkhtmltopdf, thegetBoundingClientRect().left
andoffset().left
return values such as8
for the :before element (possibly due to page margin) and0
for its parent span. - Implementing the sidebar as a
:before
pseudo-element without setting its parent torelative
. While the sidebar does appear at the left edge of the PDF in this scenario, I face challenges in determining the accurate height of the span to apply to the sidebar. For instance, a 3-line span may return a height of over a thousand pixels when usingoffsetHeight
. Similar issues arise when subtractinggetBoundingClientRect().top
fromgetBoundingClientRect().bottom
. Block elements exhibit the same behavior, suggesting that wkhtmltopdf boxes extend to the bottom of the page. - Employing the sidebar as a
:before
pseudo-element, positioned absolutely at the left edge of the page and resized by inserting line breaks into the content style to match the number of soft breaks in the span usinggetClientRects()
. Within wkhtmltopdf,getClientRects().length
may yield unexpected values, such as63
for a 3-line span. - Adding two div elements before and after detecting the span, followed by utilizing canvas to draw a line between them. However, this approach encountered roadblocks when the second div returned negative values for
offset().top
. - Regarding the JavaScript, I have experimented with both embedding it in the document and executing it from the command line using
--run-script
. Unfortunately, I encountered similar issues in both cases.
<style>
.sidebar { position: relative; background-color: yellow; }
.sidebar::before { position: absolute; content: ''; border-left: 2px #000 solid; left: 0; height: 100%; }
</style>
<body>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. <span class="sidebar">Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</span> Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</body>