Adding a PDF generated from an HTML div to an email using Java

I am looking for a solution to convert the contents of an HTML div tag into a PDF file while preserving its associated CSS. This is essential as I will be using Java on the back-end to send emails, and I need to attach the PDF with the CSS intact when sending the email.

If anyone has any advice or guidance on how I can achieve this, I would greatly appreciate it.

Answer №1

> This code sample serves as a helpful starting point to get an idea.     <?php
>     /*
>     mPDF: Creating PDFs from HTML/CSS (Full Code)
>     */
>     require_once( 'mpdf/mpdf.php'); // Including mdpf
>     $stylesheet = file_get_contents('assets/css/pdf.css'); // Retrieving css content
>     $html = '<div id="pdf-content">
>                   Insert your PDF Content here (Text/HTML)
>              </div>';
>     // Setting up the PDF
>     $mpdf = new mPDF('utf-8', 'A4-L'); // Creating new PDF object with encoding & page size
>     $mpdf->setAutoTopMargin = 'stretch'; // Setting pdf top margin to stretch to prevent content overlap
>     $mpdf->setAutoBottomMargin = 'stretch'; // Setting pdf bottom margin to stretch to prevent content overlap
>     // Adding PDF header content
>     $mpdf->SetHTMLHeader('<div class="pdf-header">
>                               <img class="left" src="assets/img/pdf_header.png"/>                      
>                           </div>'); 
>     // Adding PDF footer content                      
>     $mpdf->SetHTMLFooter('<div class="pdf-footer">
>                             <a href="http://www.lubus.in">www.lubus.in</a>
>                           </div>'); 
>      
>     $mpdf->WriteHTML($stylesheet,1); // Writing style to pdf
>     $mpdf->WriteHTML($html); // Writing html to pdf
>     // FOR EMAILING
>     $content = $mpdf->Output('', 'S'); // Saving pdf for email attachment 
>     $content = chunk_split(base64_encode($content));
>     // Email configuration
>     $mailto = $email;
>     $from_name = 'LUBUS PDF Test';
>     $from_mail = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d18101c14113d1912101c14...';
>     $replyto = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d6ded2dadff3d7dcded2da...';
>     $uid = md5(uniqid(time())); 
>     $subject = 'mdpf email with PDF';
>     $message = 'Download the attached pdf';
>     $filename = 'lubus_mpdf_demo.pdf';
>     $header = "From: ".$from_name." <".$from_mail.">\r\n";
>     $header .= "Reply-To: ".$replyto."\r\n";
>     $header .= "MIME-Version: 1.0\r\n";
>     $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
>     $header .= "This is a multi-part message in MIME format.\r\n";
>     $header .= "--".$uid."\r\n";
>     $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
>     $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
>     $header .= $message."\r\n\r\n";
>     $header .= "--".$uid."\r\n";
>     $header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
>     $header .= "Content-Transfer-Encoding: base64\r\n";
>     $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
>     $header .= $content."\r\n\r\n";
>     $header .= "--".$uid."--";
>     $is_sent = @mail($mailto, $subject, "", $header);
>     //$mpdf->Output(); // For sending Output to browser
>     $mpdf->Output('lubus_mdpf_demo.pdf','D'); // For Download
>     exit;
>     ?>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am looking to insert an array of a specific type into a Postgres database using Node.js, but I am unsure of the process

query.callfunction('fn_create_mp_product', parameters, (err, result) => { if (err) { console.log(err) callback(err); } else { if (result.status == 'success') { callb ...

What is the best way to retrieve an element that has been altered in its state?

I encountered a scenario where I want an image to have a border when clicked, and if clicked again, the border should be removed. However, the border should also be removed if another image is clicked instead. I believe there are a couple of approaches to ...

Is it possible to use Ajax post with localhost on Wamp server?

Looking to execute a basic POST function using Ajax on my localhost WAMP server. Here's the code I have: function fill_table() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari ...

Is there a way to automate the duplication/copying of files using JavaScript?

I have a GIF file stored in the "assets" directory on my computer. I want to create multiple duplicates of this file within the same directory, each with a unique filename. For example: If there is a GIF file named "0.gif" in the assets directory, I woul ...

Sending a CSS class name to a component using Next.js

I am currently in the process of transitioning from a plain ReactJS project to NextJS and I have a question. One aspect that is confusing me is how to effectively utilize CSS within NextJS. The issue I am facing involves a Button component that receives ...

How can Angular be used for live search to provide precise search results even when the server does not return data in the expected sequence?

Currently, I am in the process of constructing a website for one of my clients. The search page on the site is designed to update search results as you type in the search bar - with each keystroke triggering a new search request. However, there's an i ...

Alter the color of Material UI Raised Button when it is hovered over

I am trying to change the background color of a raised button on hover in Material UI. I attempted the following, but it did not work. Is there a way to modify the background color in Material UI for a raised button? Example.JS <RaisedButton ...

What is the secret to keeping a hover effect active?

As I hover over the buttons in my stack, a text box appears on the right side. The intention is for this text box to act as a scroll box, but it disappears when I move my mouse to scroll because it needs to be continuously hovered upon to stay active. To ...

How can I resolve the issue of <td> being repeatedly displayed five times instead of just twice in PHP?

Can someone assist me with fixing this for loop issue? I am trying to display controls next to each item in the row, but it is showing 5 sets of controls instead of just 2. <tbody> <?php //retrieve list of supplies $numOfRows = 0; $result = my ...

To demonstrate movement through both revealing and concealing

Currently, I am working on an assignment that involves rotating an image on hover. My task is to rotate the image first, then animate another object once the image rotation is complete. Upon mouse out, the initial image should return to its original positi ...

Problem with Ajax functionality on Jquery Dialog

I have implemented a Jquery.dialog feature in my application for sending messages. When the user clicks on "new", the dialog box appears, allowing them to select the recipient (currently working with IDs instead of usernames). On the first opening of the ...

What is the best way to target CSS only to elements that do not have a parent with a specific class?

Striving to preserve the existing CSS in the codebase. .my-new-class { .existing-class { ...implementing new styles } } .existing-class { ...maintaining old styles } I attempted a technique that I know won't work and understand why: ...

Is there a method to track the progress of webpage loading?

I am working on a website built with static HTML pages. My goal is to implement a full-screen loading status complete with a progress bar that indicates the page's load progress, including all images and external assets. Once the page has fully loaded ...

The $route object in vue-router appears to be empty when used with single file components

I am facing an issue while using single file components with vue-router and vue-2.0. The problem I can't seem to resolve is that the this.$route object, when called from a component, always returns empty values. For example: Messages.vue <te ...

Transform an array of strings into an array of object IDs

Recently, I encountered an issue with transforming an array of strings into object IDs using mongoose type. Unfortunately, my attempt was unsuccessful as it seems the method only works for single string inputs, not arrays. let stringObjectIdArray = [&apos ...

Tips for preventing the need to open numerous chrome windows when running multiple URLs with Selenium WebDriverJS

Is there a way to prevent multiple instances of the browser from opening when attempting to parse multiple URLs? I would like to have just one browser open and running all the URLs within it. Any advice or suggestions would be greatly appreciated! I' ...

In what way can the result of the code displayed be considered as truthful?

this.someService.findDevices() .subscribe((segments) => { this.segments = Array.from(segments.segments); this.packs.forEach((pack) => { pack.segments = Array.from(segments.segments); pack. ...

Can JavaScript functions be automated on a web browser using Power BI tables?

I am facing a challenge with saving data from a powerbi table on a daily basis. When I hover over the table and click on the ellipsis menu, a button element is generated in HTML that allows me to save the data to a CSV file using Selenium. However, achiev ...

Hide jquery scroll bar

I am currently working on a WordPress plugin with the Twenty Thirteen theme. My goal is to display a modal when a div is clicked, and at that time I want to hide the scrollbar on the body of the page. Despite trying the following code snippet, it doesn&ap ...

What is the process for using the CLI to downgrade an NPM package to a previous minor version by utilizing the tilde version tag?

I currently have Typescript version ^3.7.4 installed as a devDependency in my project's package.json: { "name": "my-awesome-package", "version": "1.0.0", "devDependencies": { "typescript": "^3.7.4" } } My goal is to downgrade Typescript ...