Esportare PDF in immagine JPEG attraverso ASP
nota bene: lo script nella versione "copia e incolla" che segue, presuppone permessi di scrittura lato server .. in paritcolare, è la root o cartella di destinazione del nuovo documento PDF che deve possedere i permessi di scrittura [chmod "666" - "777"] ..e l' utilizzo è di assoluta facilità: copia inclolla e salva come *.asp
CODICE:
<h3>AspPDF PDF verso jpeg </h3>
<FORM ENCTYPE="multipart/form-data" METHOD="POST" NAME="MainForm">
<TABLE STYLE="font-type: arial; font-size: 12">
<TR>
<TD><B>Scegli PDF </B></TD><TD><INPUT TYPE="FILE" SIZE="40" NAME="MyFile"></TD></TR>
<TR>
<TD>Inserire password se protetto</TD>
<TD><INPUT TYPE="PASSWORD" SIZE="40" NAME="Pwd"></TD>
</TR>
<TR>
<TD VALIGN="TOP">Seleziona quale pagina convertire in immagine:</TD>
<TD><INPUT TYPE="RADIO" NAME="PAGESELECT" VALUE="1" <% If Upload.Form("PAGESELECT") = "1" or Upload.Form("PAGESELECT") = "" Then Response.Write "CHECKED" %>>Prima<BR>
<INPUT TYPE="RADIO" NAME="PAGESELECT" VALUE="2" <% If Upload.Form("PAGESELECT") = "2" Then Response.Write "CHECKED" %>>ultima<BR>
<INPUT TYPE="RADIO" NAME="PAGESELECT" VALUE="3" <% If Upload.Form("PAGESELECT") = "3" Then Response.Write "CHECKED" %>>a
caso<BR>
<INPUT TYPE="RADIO" NAME="PAGESELECT" VALUE="4" <% If Upload.Form("PAGESELECT") = "4" Then Response.Write "CHECKED" %>>Altro,
specifica
<INPUT TYPE="TEXT" SIZE="2" NAME="PageNum" VALUE="<% = Upload.Form("PageNum") %>" onfocus="document.MainForm.PAGESELECT[3].checked = true"></TD>
</TR>
<TR><TD COLSPAN="2">
<INPUT TYPE="submit" VALUE="Carica PDF">
</TD></TR>
</TABLE>
</FORM>
<%
If nCount > 0 Then
Set File = Upload.Files(1)
If File.Size > 3072000 Then
Response.Write "<font color=red><b>The uploaded file exceeds 300KB. Please upload a smaller file.</b></font>"
Response.End
End If
' Open the file with AspPDF, handle exceptions
Set PDF = Server.CreateObject("Persits.PDF")
On Error Resume Next
Set Doc = PDF.OpenDocument( File.Path, Upload.Form("Pwd") )
If Err <> 0 Then
Response.Write "<b>An error occurred when opening the file " & File.FileName & ": <font color=red>" & Err.Description & "</b></font>"
Response.End
End If
' cancel On error resume next
On Error Goto 0
' If a document is secure and the password is invalid, OpenDocumen returns Nothing.
If Doc Is Nothing Then
Response.Write "<b>This document is password-protected. Please specify a valid password.</b></font>"
Response.End
End If
PageNum = 1
'
' Figure out which page the user wants to convert to PDF
if Upload.Form("PAGESELECT") = 2 Then
PageNum = Doc.Pages.Count
Elseif Upload.Form("PAGESELECT") = 3 Then
Randomize
PageNum = Int(Doc.Pages.Count * Rnd + 1)
elseif Upload.Form("PAGESELECT") = 4 Then
'response.write Upload.Form("PAGENUM")
If IsNumeric(Upload.Form("PAGENUM")) Then
PageNum = Upload.Form("PAGENUM")
if CInt(PageNum) < 1 Then PageNum = 1
if CInt(PageNum) > CInt(Doc.Pages.Count) Then PageNum = Doc.Pages.Count
End If
End if
' Now convert to page to image at 50%
On Error Resume Next
Set Preview = Doc.Pages(PageNum).ToImage("ResolutionX=36; ResolutionY=36; Debug=true")
If Err <> 0 Then
Response.Write "<b>An error occurred while converting " & PageNum & "to image: <font color=red>" & Err.Description & "</b></font>"
Response.End
End If
' cancel On error resume next
On Error Goto 0
' Save resultant image and display it
Path = Server.MapPath( "\public") & "\" & "image_" & Session.SessionID & ".jpg"
FileName = Preview.Save( Path, false)
Response.Write File.OriginalFileName & ", Page " & PageNum & ":<P><IMG BORDER=1 SRC=""/public/" & FileName & """><P>"
' Write log, if applicable.
If Preview.Log <> "" Then
Response.Write "Error log:<BR>" & Replace( Preview.Log, chr(13) & chr(10) & chr(13) & chr(10), "<br>" )
End if
End If
%>
<P>
|