Thursday 25 September 2008

Add a Convert to PDF Button to a web page





ProductVersion


  • WebGrabber


  • 3.8


NB You need activePDF Server installed to use activePDF Webgrabber

The following ASP example demonstrates the process of adding a Convert to PDF button to a webpage. Once pressed, the button initiates an activePDF WebGrabber script that converts the page to PDF.

NOTE: This example is also available for ColdFusion®. Choose from the Code Sampleslist above to display the download link and information for the script appropriate for you environment.

This example follows these necessary steps:This example includes the following files:
  • button.asp - Script to create Convert to PDF button. (Code detail provided below.)
  • index.asp - Sample page containing script. (Code detail provided below.)
  • convert.asp - Conversion script run when Convert to PDF is pressed. (Code detail provided below.)
Click here to download this example (includes listed files)



button.asp










The
script controlling the display of the button.

NOTE: This script is designed to exclude the button from the output.

<%

' Get URL of page to convert

varURL = "http://" & request.servervariables("SERVER_NAME")
&_

request.servervariables("SCRIPT_NAME") & "?button=0&"
&_

request.servervariables("QUERY_STRING")


' Collect post data for the new session

PostData = ""

For Each x in Request.Form

If varPostData = "" Then

varPostData = x & "=" & Request.Form(x)

Else

varPostData = varPostData & "&" & x & "="
& Request.Form(x)

End If

Next


' Output the form to the screen

Response.Write("<FORM NAME='PrintToPDF' METHOD='POST' ACTION='convert.asp'>")

Response.Write("<INPUT TYPE='HIDDEN' NAME='PDFURL' VALUE='"
& varURL & "'>")

Response.Write("<INPUT TYPE='HIDDEN' NAME='PDFPOST' VALUE='"
& varPostData & "'>")

Response.Write("<INPUT TYPE='SUBMIT' NAME='SUBMIT' VALUE='Convert
to PDF'>")

Response.Write("</FORM>")

%>


index.asp











The
script added to the page.
<%
' Add the button to the
page

' Check to make sure querystring button is not set
to hide


If Request.QueryString("button") = "" Then

%><!-- #include file="button.asp" --><%

End If

%>

<html>

<head>

<title>activePDF.com - WebGrabber Convert to PDF Button Example</title>

</head>



<body>

This page contains the Convert to PDF button.</body>


</html>


convert.asp





The conversion script run when the button is clicked.






The
conversion script run when the button is clicked.

<%

' Define Variables

' Get the URL to convert

varURL = Request.Form("PDFURL")



' If the URL is empty go back to default page

If varURL = "" Then

%><SCRIPT LANGUAGE="JavaScript">history.go(-1)</SCRIPT><%

End If

' Get the post data

varPostData = Request.Form("PDFPOST")



' PDF Output Folder

varPath = Server.MapPath(".")


' EngineToUse (1=IE, 0=Native)

varEngine = 0


' Timeout for various calls

varTimeout = 60



' For local machine use these defaults

varIP = "127.0.0.1"

varPort = "64320"


' Set ASP Script Timeout

Server.ScriptTimeout = varTimeout + 1


' Instantiate Objects

Set WG = Server.CreateObject("APWebGrabber.Object")

Set APS = Server.CreateObject("APServer.Object")


' Set activePDF Server properties

APS.OutputDirectory varPath

APS.PDFTimeout = varTimeout


' Must call before DoPrint to pass server settings to WG

WG.Prt2DiskSettings = APS.ToString()


' Set WebGrabber properties

WG.EngineToUse = varEngine

WG.URL = varURL

WG.HTTPPostData = varPOSTData

WG.TimeOut = varTimeout

WG.PrinterTimeout = varTimeout

If varEngine = 0 Then

WG.PrintBackgroundColors = 1

WG.TopBottomMargin = 150

WG.LeftRightMargin = 150

End If


' Start the conversion process

varReturn = WG.DoPrint(varIP, varPort)

If varReturn <> 0 Then Error("DoPrint") End If


' Wait for conversion result

varReturn = WG.Wait(varIP, varPort, varTimeout, "")

If varReturn <> 19 Then Error("Wait") End If


' Run WG CleanUp

WG.Cleanup varIP, varPort


' Get New PDF filename

Call APS.FromString(WG.Prt2DiskSettings)

varPDFName = APS.NewUniqueID


' Clear Objects

Set WG = Nothing

Set APS = Nothing


' Send the PDF to the browser

%>

<SCRIPT LANGUAGE="JavaScript">

document.location.href="<% Response.Write(varPDFName &
".PDF") %>"

</SCRIPT>

<%

' Error Handling

Sub Error(Method)

Response.Write "'" & Method & "' failed with
a '" & varReturn & "'<br>"

Response.Write "<a href='http://www.activepdf.com/"

Response.Write "support/knowledgebase/viewKb.cfm?id=10033&tk=ts'>"

Response.Write "WebGrabber Return Codes KB Article</a>"

Set WG = Nothing

Set APS = Nothing

Response.End

End Sub

%>


No comments: