function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nickfs92840nickfs92840 

PDF and Print Function on VF page

I have a java print function on my VF page, however, I want to add a PDF renderer button of some sort where the user can either print a hard copy or save the VF page as a pdf file. 

 

I used the renderas='pdf" in my current VF page, but it defaults to creating a pdf instead. 

 

Any ideas would be greately appreciated!

 

 

<apex:page standardController="Highlight_Sheet__c" standardStylesheets="false" showHeader="false" >
<apex:stylesheet value="{!URLFOR($Resource.advancedpdfsource, 'qstyles.css')}" />

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>INTERNAL USE ONLY</title>
</head>

<body class="oneColElsCtr">

<div id="container">
  <div id="mainContent">
  <p>  
  <table width="855" border="0">
      <tr>
        <td width="173"><apex:image value="https://c.na6.content.force.com/servlet/servlet.ImageServer?id=01580000000vjOP&oid=00D300000000aSY&lastMod=1332533543000" width="150" height="51"/></td>
        <td width="505"><h1 class="red">HIGHLIGHT SHEET</h1></td>
        <td width="161"><table width="99%" border="0" cellpadding="0" cellspacing="0">
  <td align="center"><a href="javascript&colon;window.print();">Print Highlight Sheet</a></td>

>>>>>>>>>>>>>>>>>>>

 

h8r41dh8r41d

I've had to create separate versions of my VF pages for HTML and PDF.  The PDF renderer is pretty basic so I had to code it separately to look right.

 

However, in your case you could use a URL parameter to dynamically specify whether to render as PDF. For example:

 

/apex/myvisualforcepage?pdf=true

 

Then in the VF do RenderAs="{!getUrlParamValue}"

 

In your controller you can just look at the param and return "PDF" or "HTML"

 

public string getUrlParamValue() {
     string urlVal =System.currentPagereference().getParameters().get('pdf');
      if(urlVal == 'true') {
          return 'PDF';
     } else {
          return 'HTML';
     }
}

 

Then what you can do is open the PDF document in a new window and use javascript to print the child window contents, and then close it.

 

Gabriel Alack
Contact Us - We Can Help!

Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x122