[SalesForce] How to attach PDF files into an email

We have a visualforce page, we want to make the visualforce page convert to PDF and saved in document and after that we want to attached the body/document as PDF on email.

The problem is that the document has no data.

Controller code:

Pagereference pg = new PageReference('/apex/ticketpdf?bookingid=' + pbookid + '&id=' + eventid);    
Blob pdf1 = pg.getcontentAsPdf(); 

Document d = new Document();
d.FolderId = '00l90000000cJAI';
d.Body = pdf1;
d.IsPublic = true;
d.Name = 'pdf_file_' + pbookid + '.pdf';
d.ContentType = 'application/pdf';
d.Type = 'pdf';
d.AuthorId = '00590000003KpijAAC';
insert d;

Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType(d.contentType);
attach.setFileName(d.name);
attach.setInline(false);
attach.Body = d.Body;

// Step 1: Create a new Email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

// Step 2: Set list of people who should get the email
List<String> sendTo = new List<String>();
sendTo.add(Email);
mail.setToAddresses(sendTo);

// Step 3: Set who the email is sent from
mail.setReplyTo('lennon@alphasys.com.au');
mail.setSenderDisplayName('Alphasys iNatural - ProntoEvents');

mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 

This is the VF page:

<apex:page standardStylesheets="false" showHeader="false" applyBodyTag="false" applyHtmlTag="false" sidebar="false" renderAs="pdf" docType="html-5.0" controller="EventsController">
    <head>
        <title></title>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
        <apex:stylesheet value="https://fonts.googleapis.com/css?family=Open+Sans:400,600"/>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

        </head>
    <apex:repeat var="ticket" value="{!ticketpdf}">
    <div class="ticket-container">
        <div class="ticket">
            <div class="ticket-head">
                <h5>This serves as an invitation</h5>
                <!--h2>John Mark Pajardo</h2-->
            </div>
            <div class="ticket-content">
                <div class="qr-code">
                    <!--<img style="-webkit-user-select: none" src="http://chart.googleapis.com/chart?cht=qr&amp;chl=Lennon+Talosi&amp;chs=200x200&amp;choe=UTF-8"/>-->
                    <p class="qr-code">{!ticket.Ticket_Number__c}</p>
                </div>
                <h2>{!ticket.Event__r.Event_Name__c}</h2>
                <div class="line"></div>
                <p class="description">Please present your Booking Receipt QR Code when you arrive at the event.</p>    
            </div>
            <div class="ticket-link">
                <p>Event Link: <a href="#">http://prontroevents-developer-edition.ap1.force.com/eventregistration?id={!ticket.Event__c}</a></p>
            </div>
        </div>
    </div>

        </apex:repeat>
</apex:page>

This is the apex code of the vf page:

public List<Ticket__c>  getTicketPDF(){

        String eventId = ApexPages.currentPage().getParameters().get('id');

    String pdfID = ApexPages.currentPage().getParameters().get('bookingid');


    List<Ticket__c>  ticketpdf = [SELECT Id, Name, Event__r.Event_Name__c,Ticket_Number__c,Event__c FROM Ticket__c Where Booking_ID__c = :pdfID AND Event__c = :eventId];

    return ticketpdf;

}

Best Answer

You have a several problems I see that are preventing your document from rendering.

First, your source visualforce page is docType="html-5.0". To the best of my knowledge, the current render engine Flying Saucer doesn't support html 5.0. It definitely doesn't support any active content nor any CSS beyond CSS 2.1.

Another very specific issue to your code is that you've embedded PDFs as images. That's something else that's not supported. Those images are actually JPGs embedded inside the PDFs which the render engine doesn't know how to extract since it can't deconstruct another PDF. Your browser's PDF plug-in is what displays it for you when viewed as a web page. You could attach those PDFs separately, but you're not going to be able to embed them within your visualforce page if you want to render it as a PDF at this time.