Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

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

%>


Thursday, 21 August 2008

DocConverter Simple GUI for File Conversion

The following VB.NET example demonstrates a basic GUI implementation of activePDF DocConverter. The interface provides an easy way for the user to convert a file to PDF and specify the location for DocConverter's common setup requirements.

Required Components

NOTE: You must reference the APDocConvNET.DLL in order to run the example. Refer to KB #9 for instructions.

Click here to download this example (does not include DLL).





Figure 1.1: DocConverter Example GUI.

The GUI has these required settings:

  • File to Convert: The full path to the file to convert to PDF.
    NOTE: The corresponding DocConverter Processors must be installed for best conversion quality.
  • Folder to Place PDF: The full path for the location to place the output PDF.
  • Folder to Place Error Files: The full path where any files that are unable to convert are placed.
  • Folder to Place Original Document: The full path to store the original input file after conversion.
  • IP of DocConverter Machine: The IP address for the machine where DocConverter is installed (Defaults to 127.0.0.1).
  • Port (0 for Default): The port assigned to the Submit and CheckStatus methods (Defaults to 0 for 53540).
  • Timeout (Seconds): The timeout in seconds (Defaults to 30).