Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Convert html page to pdf and email

Posted on 2014-08-14
2
Medium Priority
?
628 Views
Last Modified: 2014-08-27
I have a webpage that I need to add a button to that will open up a print friendly version of the page in PDF and attach it to an email.

I don't want to have to print as pdf and then open the email and attach it. I was hoping there was something out there where the user could push a button and it would convert the page to pdf, open up their email client, and attach the document.

Is it just wishful thinking?
0
Comment
Question by:BHUC
2 Comments
 
LVL 6

Expert Comment

by:Ravi Kalla
ID: 40261790
There are two steps -
1. On click of a button, you can hit a server side java code that converts the HTML page to PDF as iText.
2. Open email client and attach PDF document to it -
        a) You can use javascript to read PDF content from backend and open email client and attach it.
        b) Please check this link to open email and attach PDF document to it.
        c) Without opening email client, you can alternatively send the email from the backend itself using javamail API.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40265060
As a practical matter it's just wishful thinking.  Let me try to explain.

HTML is a semantic markup language which attaches meaning to content.  HTML does not care about layout, just about meaning of the content.  (Two decades ago, there were no really good layout options for web pages, and HTML was "pressed into service."  Today we have CSS for electronic document layout, and nobody uses HTML tables for layout any more.)

in contrast, PDF is a document layout for the printed page.  It does not care at all about the meaning of the content; it just puts the information precisely into the page layout.

Semantic markup and document layout are not quite as different as fish and bicycles, but almost.  If you prepare a document for the web, the process is very different from preparing a document for print.  In PHP we have two free and open source libraries that are popularly used for preparing PDF documents.  They are FPDF and TCPDF.  Download one of them and run with it.  Neither one is "drop-dead easy" but they can both be mastered with a little trial and error, and once you have the document layout completed, both libraries are lightning fast.  To help you get started, here is the FPDF "Hello World" script that shows some of the basics.

<?php // demo/fpdf_hello_world.php
error_reporting(E_ALL);


// DEMONSTRATE SOME OF THE BASICS OF FPDF


// SOME VARIABLES FOR OUR TESTS (COULD COME FROM DATA BASE, ETC)
$font = 'Arial';
$text = 'Hello World!';

// BRING IN THE PDF THING
require_once('fpdf16/fpdf.php');

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'throwaway'
. DIRECTORY_SEPARATOR
. 'temp_pdf_blue'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN WHITE ON A BLUE BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 16);
$pdf->SetFillColor(  0,   0, 255);
$pdf->SetTextColor(255, 255, 255);
$pdf->Cell(40, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Blue PDF</a>';



// START OVER FOR A NEW PDF
unset($pdf);
echo PHP_EOL . "<br/>";

// SYNTHESIZE THE PDF FILE INFORMATION
$pdf_file_link
= 'throwaway'
. DIRECTORY_SEPARATOR
. 'temp_pdf_red'
. '.pdf'
;
$pdf_file_name
= getcwd()
. DIRECTORY_SEPARATOR
. $pdf_file_link
;

// DO THE HELLO WORLD EXERCISE IN BLACK ON A RED BACKGROUND
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont($font, 'B', 24);
$pdf->SetFillColor(255,   0,   0);
$pdf->SetTextColor(  2,   2,   2);
$pdf->Cell(52, 10, $text, 0, 2, 'L', TRUE);

// WRITE THE PDF TO DISK
$pdf->Output($pdf_file_name, 'F');

// PRESENT A LINK
echo '<a target="my_PDF" href="' . $pdf_file_link . '">Red PDF</a>';

Open in new window

HTH, ~Ray
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question