VB.NET Application Crystal Report to PDF converter with ftp uploader

 Crystal Report to PDF converter with ftp uploader

This Application converts crystal report with mysql database in to pdf and upload pdf to ftp server running on linux.

1. Create the ODBC connection for mysql from control panel>admin tools> odbc manager. detail http://vinodkotiya.blogspot.com/2011/08/vbnet-with-crystal-report-and-mysql.html

2. in all of crystal report u should have the above connection in rpt files while creating them in crystal designer.

3. put all rpt files in some report folder. Do not have any direct reference to these rpt file in ur vb.net application. The application will scan the report folder and convert all rpt file in to pdf file and put them in pdf folder while uploading via ftp. It will also make a log.

4. open a new web application and use following code.

Imports CrystalDecisions.CrystalReports.Engine


Imports CrystalDecisions.Shared


Imports System.IO


Imports System.Net


Public Class Form1






Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


'CRtoPDF("mou_summary")


'FtpUploadFileToServer("191.254.1.42", Application.StartupPath & "\" & "mou_summary" & ".pdf", "/home/admin", , "admin", "admin&123", , True)






'CRtoPDF("WebMilesIssues")


'FtpUploadFileToServer("191.254.1.42", Application.StartupPath & "\" & "WebMilesIssues" & ".pdf", "/home/admin", , "admin", "admin&123", , True)










'makeLog()


'Me.Close()


startjob()


makeLog()


Me.Close()


End Sub


Private Function startjob() As Boolean


'Dim cr As New ReportDocument


Dim cr As ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument()


Dim di As New IO.DirectoryInfo(Application.StartupPath & "\reports")


Dim aryFi As IO.FileInfo() = di.GetFiles("*.rpt")


Dim fi As IO.FileInfo






For Each fi In aryFi


Try


'' lblStatus.Text = lblStatus.Text & fi.Name & vbCrLf


lblStatus.Text = lblStatus.Text & " <-Converting " & fi.FullName


'Console.WriteLine("File Size (KB): {0}", strFileSize)


'Console.WriteLine("File Extension: {0}", fi.Extension)


'Console.WriteLine("Last Accessed: {0}", fi.LastAccessTime)


'Console.WriteLine("Read Only: {0}", (fi.Attributes.ReadOnly = True).ToString)


Dim thefile As String = fi.FullName ''Application.StartupPath & "\reports\CrystalReport2.rpt"


cr.Load(thefile)


'CrystalReportViewer1.ReportSource = cr


'CrystalReportViewer1.Refresh()










''code for export


Dim x_opt As ExportOptions = cr.ExportOptions


x_opt.ExportFormatType = ExportFormatType.PortableDocFormat


x_opt.ExportDestinationType = ExportDestinationType.DiskFile


x_opt.DestinationOptions = New DiskFileDestinationOptions()


Dim hdd_opt As New DiskFileDestinationOptions()






CType(cr.ExportOptions.DestinationOptions, DiskFileDestinationOptions).DiskFileName = (Application.StartupPath & "\pdf\" & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf")


cr.Export()


lblStatus.Text = lblStatus.Text & " Created " & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf "


'start upload


FtpUploadFileToServer("191.254.1.42", Application.StartupPath & "\pdf\" & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf", "/home/admin", , "admin", "pwd", , True)






Catch e As Exception


lblStatus.Text = lblStatus.Text & " Error: " & e.Message & " for " & Strings.Left(fi.Name, Len(fi.Name) - 4) & ".pdf ->"


End Try






Next










End Function


Private Function FtpUploadFileToServer(ByVal pServer As String, _


ByVal pUploadPathAndFileName As String, _


Optional ByVal pTargetPath As String = "", _


Optional ByVal pTargetFileName As String = "", _


Optional ByVal pUserName As String = "", _


Optional ByVal pPassword As String = "", _


Optional ByVal pPort As Integer = 21, _


Optional ByVal pUsePassive As Boolean = False) As Boolean


Dim objUploadStream As FileStream = Nothing


Dim objRequest As FtpWebRequest = Nothing


Dim objResponse As FtpWebResponse = Nothing


Dim objRequestStream As Stream = Nothing


Try


lblStatus.Text = lblStatus.Text & " initiating upload " & pUploadPathAndFileName


objUploadStream = File.OpenRead(pUploadPathAndFileName)


Dim bytBuffer(CType(objUploadStream.Length, Integer)) As Byte


objUploadStream.Read(bytBuffer, 0, bytBuffer.Length)






If pTargetFileName.Length = 0 Then


pTargetFileName = IO.Path.GetFileName(objUploadStream.Name)


End If


Dim strUrl As String = String.Format("ftp://{0}:{1}/{2}/{3}", _


pServer, pPort, pTargetPath, pTargetFileName)


objRequest = CType(FtpWebRequest.Create(strUrl), FtpWebRequest)


If pUserName.Length > 0 And pPassword.Length > 0 Then


objRequest.Credentials = New NetworkCredential(pUserName, pPassword)


End If


objRequest.Method = WebRequestMethods.Ftp.UploadFile


objRequest.Proxy = Nothing


objRequest.KeepAlive = False


objRequest.UsePassive = pUsePassive






objRequestStream = objRequest.GetRequestStream()


objRequestStream.Write(bytBuffer, 0, bytBuffer.Length)


objRequestStream.Close()






objResponse = CType(objRequest.GetResponse, FtpWebResponse)


'MsgBox(objResponse.StatusDescription)


lblStatus.Text = lblStatus.Text & " - " & objResponse.StatusDescription & " at " & Now.TimeOfDay.ToString & " ->"






Catch ex As Exception


'MsgBox(ex.Message)


lblStatus.Text = lblStatus.Text & ex.Message & "->"


Finally


Try


If Not objRequestStream Is Nothing Then


objRequestStream.Close()


End If


If Not objUploadStream Is Nothing Then


objUploadStream.Close()


objUploadStream.Dispose()


End If


If Not objRequest Is Nothing Then


objRequest = Nothing


End If






Catch ex As Exception


'MsgBox(ex.Message)


lblStatus.Text = lblStatus.Text & vbCrLf & ex.Message


End Try


End Try


End Function


Private Function makeLog() As Boolean


Dim path As String = "log.txt"


Dim sw As StreamWriter






' This text is added only once to the file.


If File.Exists(path) = False Then


' Create a file to write to.


sw = File.CreateText(path)


sw.Flush()


sw.Close()






End If






' This text is always added, making the file longer over time


' if it is not deleted.


sw = File.AppendText(path)


sw.WriteLine("<<<<<<<<<<<<<<<")


sw.WriteLine(lblStatus.Text)


sw.WriteLine(">>>>>>>>>>>>>>>")


sw.Flush()


sw.Close()






'' Open the file to read from.


'Dim sr As StreamReader = File.OpenText(path)


'Dim s As String


'Do While sr.Peek() >= 0


' s = sr.ReadLine()


' Console.WriteLine(s)

'Loop


'sr.Close()


End Function
 
- Vinod Kotiya

Comments

darichkid said…
This has to be my favorite FTP Library for VB.NET:
https://www.kellermansoftware.com/p-39-net-ftp-library.aspx