<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[The Debug Diaries]]></title><description><![CDATA[Jangachary Sriramadas, Web Developer extraordinaire! Expert in Java, JS, and frameworks. Crafting seamless web experiences and exploring AI prompts. Let's build]]></description><link>https://thedebugdiaries.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 02 Sep 2026 23:21:47 GMT</lastBuildDate><atom:link href="https://thedebugdiaries.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Converting HTML to PDF in Java Using Flying Saucer]]></title><description><![CDATA[In today's digital world, converting HTML files to PDF documents programmatically is a common requirement, especially in enterprise applications dealing with report generation, document archival, and more. In this blog post, we'll explore how to acco...]]></description><link>https://thedebugdiaries.hashnode.dev/converting-html-to-pdf-in-java-using-flying-saucer</link><guid isPermaLink="true">https://thedebugdiaries.hashnode.dev/converting-html-to-pdf-in-java-using-flying-saucer</guid><category><![CDATA[Java]]></category><category><![CDATA[pdf]]></category><category><![CDATA[HTML5]]></category><dc:creator><![CDATA[Jangachary]]></dc:creator><pubDate>Fri, 08 Mar 2024 13:50:54 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1710144257116/1e673a7b-2b5b-4fe5-a9a4-4d75bd9d7f39.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>In today's digital world, converting HTML files to PDF documents programmatically is a common requirement, especially in enterprise applications dealing with report generation, document archival, and more. In this blog post, we'll explore how to accomplish this task using Java with the help of Flying Saucer library along with Maven dependencies.</p>
<h2 id="heading-introduction-to-flying-saucer">Introduction to Flying Saucer</h2>
<p><a target="_blank" href="https://github.com/flyingsaucerproject/flyingsaucer">Flying Saucer</a> is an open-source Java library that provides an easy way to convert XHTML/XML documents to PDF. It utilizes the powerful iText library under the hood for PDF generation.</p>
<h2 id="heading-setting-up-maven-dependencies">Setting Up Maven Dependencies</h2>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.xhtmlrenderer<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>flying-saucer-core<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>9.1.22<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.xhtmlrenderer<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>flying-saucer-pdf<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>9.1.22<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.xhtmlrenderer<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>flying-saucer-pdf-openpdf<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>9.1.20<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>org.jsoup<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>jsoup<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.13.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
</code></pre>
<p>These dependencies include the core Flying Saucer library, PDF rendering functionality, Flying Saucer PDF implementation using OpenPDF, and Jsoup library for HTML parsing.</p>
<h2 id="heading-converting-html-to-pdf-java-code">Converting HTML to PDF - Java Code</h2>
<p>Now, let's dive into the Java code that performs the conversion:</p>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.FileOutputStream;
<span class="hljs-keyword">import</span> java.nio.charset.StandardCharsets;
<span class="hljs-keyword">import</span> java.nio.file.Files;
<span class="hljs-keyword">import</span> java.nio.file.Paths;
<span class="hljs-keyword">import</span> java.util.HashMap;
<span class="hljs-keyword">import</span> java.util.Map;
<span class="hljs-keyword">import</span> java.util.Map.Entry;
<span class="hljs-keyword">import</span> java.util.Set;

<span class="hljs-keyword">import</span> org.apache.commons.io.FileUtils;
<span class="hljs-keyword">import</span> org.jsoup.Jsoup;
<span class="hljs-keyword">import</span> org.jsoup.nodes.Document;
<span class="hljs-keyword">import</span> org.xhtmlrenderer.pdf.ITextRenderer;

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PdfGenerator</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> String <span class="hljs-title">htmlToXhtml</span><span class="hljs-params">(String html)</span> </span>{
        <span class="hljs-comment">// Convert HTML to XHTML</span>
        Document document = Jsoup.parse(html);
        document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);
        <span class="hljs-keyword">return</span> document.html();
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        String inputFile = <span class="hljs-string">"appointment_letter.html"</span>; <span class="hljs-comment">// Path to your XHTML/XML file</span>
        String outputFile = <span class="hljs-string">"output.pdf"</span>; <span class="hljs-comment">// Path to the output PDF file</span>

        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">// Create an ITextRenderer instance</span>
            ITextRenderer renderer = <span class="hljs-keyword">new</span> ITextRenderer();

            <span class="hljs-comment">// Read HTML content from file</span>
            String content = FileUtils.readFileToString(Paths.get(inputFile).toFile(), StandardCharsets.UTF_8);

            <span class="hljs-comment">// Perform replacements</span>
            Map&lt;String, String&gt; valueMap = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();
            valueMap.put(<span class="hljs-string">"employeeId"</span>, <span class="hljs-string">"20240200001"</span>);
            valueMap.put(<span class="hljs-string">"employeeName"</span>, <span class="hljs-string">"Harish Jay Raj"</span>);
            valueMap.put(<span class="hljs-string">"startDate"</span>, <span class="hljs-string">"25-03-2024"</span>);

            Set&lt;Entry&lt;String, String&gt;&gt; entrySet = valueMap.entrySet();
            <span class="hljs-keyword">for</span> (Entry&lt;String, String&gt; es : entrySet) {
                content = content.replace(<span class="hljs-string">"@{"</span> + es.getKey() + <span class="hljs-string">"}"</span>, es.getValue());
            }

            <span class="hljs-comment">// Convert HTML to XHTML</span>
            String htmlToXhtml = PdfGenerator.htmlToXhtml(content);
            renderer.setDocumentFromString(htmlToXhtml);

            <span class="hljs-comment">// Render the document to PDF</span>
            renderer.layout();
            FileOutputStream fos = <span class="hljs-keyword">new</span> FileOutputStream(outputFile);
            renderer.createPDF(fos);
            fos.close();

            System.out.println(<span class="hljs-string">"PDF generated successfully!"</span>);
        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        }
    }
}
</code></pre>
<h2 id="heading-understanding-the-code">Understanding the Code</h2>
<ul>
<li><p>We use <strong>ITextRenderer</strong> class from Flying Saucer to render HTML content to PDF.</p>
</li>
<li><p>The HTML content is read from a file (appointment_letter.html in this case) and stored as a string.</p>
</li>
<li><p>We perform dynamic content replacement using a Map to replace placeholders like @{employeeId} and @{employeeName} with actual values.</p>
</li>
<li><p>The HTML content is converted to XHTML using Jsoup library to ensure compatibility with Flying Saucer.Finally, the XHTML content is rendered to PDF and saved to the specified output file (output.pdf).</p>
</li>
</ul>
<p>Sample appointment.html</p>
<pre><code class="lang-xml"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Appointment Letter<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
        <span class="hljs-selector-tag">body</span> {
            <span class="hljs-attribute">font-family</span>: Arial, sans-serif;
        }

        <span class="hljs-selector-class">.letter</span> {
            <span class="hljs-attribute">max-width</span>: <span class="hljs-number">600px</span>;
            <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
            <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>;
            <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#ccc</span>;
            <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
            <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">10px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.1</span>);
        }

        <span class="hljs-selector-class">.header</span> {
            <span class="hljs-attribute">text-align</span>: center;
            <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">20px</span>;
        }

        <span class="hljs-selector-class">.employee-info</span> {
            <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">20px</span>;
        }

        <span class="hljs-selector-class">.closing</span> {
            <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">20px</span>;
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"letter"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"header"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Appointment Letter<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
            Dear <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"employeeName"</span>&gt;</span>@{employeeName}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>,
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
            We are pleased to inform you that you have been appointed as an employee at XYZ Company. Your dedication and skills have earned you the position with the following details:
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"employee-info"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span>Employee ID:<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"employeeId"</span>&gt;</span>@{employeeId}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
            Your employment with XYZ Company will commence on <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"startDate"</span>&gt;</span>@{startDate}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>. Please report to the HR department on your joining date for further orientation and formalities.
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>
            We believe that your expertise will contribute significantly to our team, and we look forward to your valuable contributions.
        <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"closing"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Best regards,<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>The XYZ Company Team<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Below is generated PDF file screenshot.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1709905457642/c58702f7-a8fe-4834-a303-52acccc0bd8c.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, we explored how to convert HTML files to PDF documents using Java with Flying Saucer library. This approach offers flexibility and customization options for generating PDF documents from HTML content programmatically. By following the provided steps and code snippets, you can easily integrate HTML to PDF conversion functionality into your Java applications.</p>
]]></content:encoded></item><item><title><![CDATA[Welcome to The Debug Diaries, Web Dev Tips, Tricks, and Tales of Triumph]]></title><description><![CDATA[Hey there, code crew!
Welcome to The Debug Diaries, your cozy corner for all things web development. Whether you're a coding superhero or just starting to learn, this is your spot to share, learn, and grow.
The web? It's like a giant playground where...]]></description><link>https://thedebugdiaries.hashnode.dev/welcome-to-the-debug-diaries-web-dev-tips-tricks-and-tales-of-triumph</link><guid isPermaLink="true">https://thedebugdiaries.hashnode.dev/welcome-to-the-debug-diaries-web-dev-tips-tricks-and-tales-of-triumph</guid><category><![CDATA[blog]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Java]]></category><dc:creator><![CDATA[Jangachary]]></dc:creator><pubDate>Mon, 22 Jan 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/zNRITe8NPqY/upload/e68d0976502b2a518f03ac18a290151d.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Hey there, code crew!</p>
<p>Welcome to <strong>The Debug Diaries</strong>, your cozy corner for all things web development. Whether you're a coding superhero or just starting to learn, this is your spot to share, learn, and grow.</p>
<p>The web? It's like a giant playground where ideas become reality. Websites, apps, games – everything you do online? Web developers built it! It's a mix of creativity, problem-solving, and maybe a little too much coffee (we'll get to that!).</p>
<p>So, what's the deal with this blog?</p>
<p>We'll spill the tea on everything:</p>
<ul>
<li><p><strong>Bug Busting Adventures:</strong> We'll share our battle stories with tricky errors and annoying glitches. No bug is too scary, no challenge too big! We'll slay them all, together. ⚔️</p>
</li>
<li><p><strong>Coding Confessions:</strong> We'll get real about the doubts, the late-night coding marathons, and the occasional "what am I even doing?" moments. Trust us, you're not alone in this!</p>
</li>
<li><p><strong>Web Dev Wonders:</strong> We'll explore the cool new stuff happening in the web world, from fancy frameworks to mind-blowing design tricks. Stay ahead of the curve and impress your friends!</p>
</li>
<li><p><strong>Community Corner:</strong> This is your hangout spot! Ask questions, share your experiences, and connect with other code warriors. We're all in this together, let's build a community that kicks code!</p>
</li>
</ul>
<p>So, grab your favorite drink, put on your coding hat, and dive into The Debug Diaries! We're excited to have you on this code-tastic journey.</p>
<p>Happy coding!</p>
<p>The Debug Diaries Team.</p>
]]></content:encoded></item></channel></rss>