You are viewing limited content. For full access, please sign in.

Question

Question

Export JPG and Generate Pages Using Import Agent

asked on September 22, 2017 Show version history

I have a workflow that creates an entry, then makes a web request that grabs image binary data, and then attaches it to the created entry. 

This works great, the issue I'm running into is viewing the image inside the repo using the laserfiche viewer. It does not display an image when I open it because it's not generating pages for it. I know I can generate pages for it but I'm grabbing a ton of pictures and manually doing it is not an option. So I figured, I could setup quick fields agent to run after this workflow runs to generate pages for it. That doesn't work. I then did some digging around on this site and found a couple of scripts to export the image as a pdf but that's not working either, it makes a blank pdf. I then tried importing it using import agent and that was successfully making pages for it. So now, my question is; How can I export this jpg to a folder and possibly maintain the metadata already generated from the workflow (I have an idea on this already, but I'm open to suggestions)? I saw something about using DocumentExporter.ExportElecDoc but there was no code attached to it and I'm not familiar enough to build that script out. Thank you guys in advance for any help, I really appreciate it!

 

EDIT: I've already asked the third-party company if it's possible to receive the images in a TIFF format but it is not at this time. 

0 0

Answer

SELECTED ANSWER
replied on September 29, 2017

Since I have no idea what format the file is in, but it does work to attach it as eDoc, then you can use this script to convert the eDoc to Laserfiche Doc.

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports Laserfiche.DocumentServices
Imports Laserfiche.RepositoryAccess

Namespace WorkflowActivity.Scripting.SDKScript
    '''<summary>
    '''Provides one or more methods that can be run when the workflow scripting activity is performed.
    '''</summary>
    Public Class Script1
        Inherits RAScriptClass102

        Private sError as String = Nothing

        '''<summary>
        '''This method is run when the activity is performed.
        '''</summary>
        Protected Overrides Sub Execute()
            'Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            If BoundEntryInfo.EntryType = EntryType.Document Then
                Proc(BoundEntryId)
            End If
            SetTokenValue("ScriptError", sError)
        End Sub

        Private Sub RemoveEDoc(ByVal iEntryID As Integer)
            Try
                Using CurDoc As DocumentInfo = Document.GetDocumentInfo(iEntryID, RASession)
                    CurDoc.DeleteEdoc()
                End Using
            Catch ex As Exception
                sError = System.Reflection.MethodBase.GetCurrentMethod.Name & ": " & ex.Message
            End Try
        End Sub

        Private Function ImportImage(ByVal iEntryID As Integer, ByVal msImage As IO.MemoryStream) As Boolean
            Dim bReturn As Boolean = False
            Try
                Using CurDoc As DocumentInfo = Document.GetDocumentInfo(iEntryID, RASession)
                    Dim CurImporter As DocumentImporter = New DocumentImporter()
                    CurImporter.Document = CurDoc
                    CurImporter.OcrImages = True
                    CurImporter.PagePosition = -1
                    CurImporter.ImportImages(msImage)
                End Using
                bReturn = True
            Catch ex As Exception
                sError = System.Reflection.MethodBase.GetCurrentMethod.Name & ": " & ex.Message
                bReturn = False
            End Try
            Return bReturn
        End Function

        Private Function ExportEImage(ByVal iEntryID As Integer) As IO.MemoryStream
            Dim ms As IO.MemoryStream = Nothing
            Try
                Using CurDoc As DocumentInfo = Document.GetDocumentInfo(iEntryID, RASession)
                    ms = New IO.MemoryStream
                    Dim CurExporter As DocumentExporter = New DocumentExporter()
                    CurExporter.ExportElecDoc(CurDoc, ms)
                End Using
            Catch ex As Exception
                sError = System.Reflection.MethodBase.GetCurrentMethod.Name & ": " & ex.Message
                ms = Nothing
            End Try
            Return ms
        End Function

        Private Sub Proc(ByVal LFEntryID As Integer)
            Using MemStrm As IO.MemoryStream = ExportEImage(LFEntryID)
                If MemStrm IsNot Nothing Then
                    If ImportImage(LFEntryID, MemStrm) Then
                        RemoveEDoc(LFEntryID)
                    End If
                End If
            End Using
        End Sub

    End Class
End Namespace

 

1 0

Replies

replied on September 22, 2017 Show version history

The problem is that in your workflow you are using an "Attach Electronic Document" activity.  This brings in the image as an electronic document rather and a Laserfiche document page.  instead of the attach electronic document, you should use a SDK script activity to add the image as a page to the document.

Something like this should work, but make sure to set the SDK Script Activity to the Create Entry Output.

            'Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            Dim sError as String = Nothing
            ' Get path to image file
            Dim sFile As String = GetTokenValue("ImageFilePathTokenName")
            SetTokenValue("ImageFilePath", sFile)
            Try
                ' Only process if the image file is available
                Dim f As New System.IO.FileInfo(sFile)
                If f.Exists Then
                    ' Create new DocumentInfo object to import into
                    Dim docInfo As DocumentInfo = DirectCast(BoundEntryInfo, DocumentInfo)
                    ' Create DocumentImporter object
                    Dim docImporter As DocumentImporter = New DocumentImporter
                    ' Set DocumentImporter target
                    docImporter.Document = docInfo
                    ' Set the Document importer OCR option
                    docImporter.OcrImages = True
                    ' Set Importer to put page at end of document
                    docImporter.PagePosition = -1
                    ' Import the Image file
                    docImporter.ImportImages(sFile)
                    ' Dispose the DocumentInfo object
                    docInfo.Dispose()
                Else
                    sError = "Image file does not exist"
                End If
            Catch ex As Exception
                sError = ex.Message
            End Try
            If String.IsNullOrEmpty(sError) Then
                sError = "None"
            End If
            SetTokenValue("ScriptError", sError)

1 0
replied on September 27, 2017

I finally got a chance to try this out and while it is making a laserfiche document, it is not attaching the photo data to that file. 

I'm guessing it's probably something to do with line 23 or 24. Line 23 is just the name of the file? I'm not sure what should go there. I'm assuming line 24 is the actual web request content that has the photo data, correct? 

0 0
replied on September 27, 2017 Show version history

Put in a "Track Tokens" activity after the Script activity and set it to track the tokens of the script.  Does the "ScriptError" token have anything other than "None" in it?

0 0
replied on September 27, 2017

The error is "Illegal characters in path," when I use the photo data on line 23. When I use the output entry name the error is "image file does not exist."

0 0
replied on September 27, 2017

The script (in line 24) is also creating a token for what you are retrieving as the path.

Being that it is a web request, I am guessing that it is a web address.  You will have to take that token and convert it to a windows or UNC path to the image for the script to be able to get the file.

0 0
replied on September 27, 2017

I'm not sure I follow. The web request just grabs the image data, there is no way for me to get the file path. Would I need to save it as an image first and then pass it to the script? So attach it to the electronic document then run the script on that?

0 0
replied on September 27, 2017

Then you will have to figure out exactly what format you are getting it in (my guess is a base64 string) and convert it to a valid image stream.  Then you can load the stream into the document, but my code sample does not do that.  My sample takes an image from a windows path and imports it.

0 0
replied on September 27, 2017

When I attach it to the electronic file, I can open the image and see it using windows image preview. Can I just point your script to the file that results from attaching it using "attach electronic document"?

0 0
replied on September 27, 2017

Maybe the file is coming in a stream object.  You can try to change the code to this and see.

        'Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
        Dim sError As String = Nothing
        ' Get path to image file
        Dim oFile As Object = GetTokenValue("HTTPWebRequest7_Content")
        SetTokenValue("HTTPWebRequest7_Type", oFile.GetType.ToString())
        Try
            ' Create new DocumentInfo object to import into
            Dim docInfo As DocumentInfo = DirectCast(BoundEntryInfo, DocumentInfo)
            ' Create DocumentImporter object
            Dim docImporter As DocumentImporter = New DocumentImporter
            ' Set DocumentImporter target
            docImporter.Document = docInfo
            ' Set the Document importer OCR option
            docImporter.OcrImages = True
            ' Set Importer to put page at end of document
            docImporter.PagePosition = -1
            ' Import the Image file
            docImporter.ImportImages(oFile)
            ' Dispose the DocumentInfo object
            docInfo.Dispose()
        Catch ex As Exception
            sError = ex.Message
        End Try
        If String.IsNullOrEmpty(sError) Then
            sError = "None"
        End If
        SetTokenValue("ScriptError", sError)

 

0 0
replied on September 28, 2017

No go, this is what I'm getting now. 

0 0
replied on September 28, 2017 Show version history

This token makes me believe that the data in the HTTPWebRequest7_Content is a base64 encoded string.  Therefore, you must try to convert it to a stream before you pass it into the DocumentImporter.

            'Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            Dim sError As String = Nothing
            ' Get path to image file
            Dim sBase64File As String = GetTokenValue("HTTPWebRequest7_Content")
            SetTokenValue("HTTPWebRequest7_Content", sBase64File)
            Try
                Dim RawFileBuffer() as Byte = Convert.FromBase64String(sBase64File)
                using FileMemStream As MemoryStream = New MemoryStream(RawFileBuffer))
                    ' Create new DocumentInfo object to import into
                    Dim docInfo As DocumentInfo = DirectCast(BoundEntryInfo, DocumentInfo)
                    ' Create DocumentImporter object
                    Dim docImporter As DocumentImporter = New DocumentImporter
                    ' Set DocumentImporter target
                    docImporter.Document = docInfo
                    ' Set the Document importer OCR option
                    docImporter.OcrImages = True
                    ' Set Importer to put page at end of document
                    docImporter.PagePosition = -1
                    ' Import the Image file
                    docImporter.ImportImages(FileMemStream)
                    ' Dispose the DocumentInfo object
                    docInfo.Dispose()
                End Using
            Catch ex As Exception
                sError = ex.Message
            End Try
            If String.IsNullOrEmpty(sError) Then
                sError = "None"
            End If
            SetTokenValue("ScriptError", sError)

Post back any ScriptError as well as the HTTPWebRequest7_Content token

0 0
replied on September 29, 2017

This is what I'm getting with the new script. 

0 0
SELECTED ANSWER
replied on September 29, 2017

Since I have no idea what format the file is in, but it does work to attach it as eDoc, then you can use this script to convert the eDoc to Laserfiche Doc.

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Imports Laserfiche.DocumentServices
Imports Laserfiche.RepositoryAccess

Namespace WorkflowActivity.Scripting.SDKScript
    '''<summary>
    '''Provides one or more methods that can be run when the workflow scripting activity is performed.
    '''</summary>
    Public Class Script1
        Inherits RAScriptClass102

        Private sError as String = Nothing

        '''<summary>
        '''This method is run when the activity is performed.
        '''</summary>
        Protected Overrides Sub Execute()
            'Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            If BoundEntryInfo.EntryType = EntryType.Document Then
                Proc(BoundEntryId)
            End If
            SetTokenValue("ScriptError", sError)
        End Sub

        Private Sub RemoveEDoc(ByVal iEntryID As Integer)
            Try
                Using CurDoc As DocumentInfo = Document.GetDocumentInfo(iEntryID, RASession)
                    CurDoc.DeleteEdoc()
                End Using
            Catch ex As Exception
                sError = System.Reflection.MethodBase.GetCurrentMethod.Name & ": " & ex.Message
            End Try
        End Sub

        Private Function ImportImage(ByVal iEntryID As Integer, ByVal msImage As IO.MemoryStream) As Boolean
            Dim bReturn As Boolean = False
            Try
                Using CurDoc As DocumentInfo = Document.GetDocumentInfo(iEntryID, RASession)
                    Dim CurImporter As DocumentImporter = New DocumentImporter()
                    CurImporter.Document = CurDoc
                    CurImporter.OcrImages = True
                    CurImporter.PagePosition = -1
                    CurImporter.ImportImages(msImage)
                End Using
                bReturn = True
            Catch ex As Exception
                sError = System.Reflection.MethodBase.GetCurrentMethod.Name & ": " & ex.Message
                bReturn = False
            End Try
            Return bReturn
        End Function

        Private Function ExportEImage(ByVal iEntryID As Integer) As IO.MemoryStream
            Dim ms As IO.MemoryStream = Nothing
            Try
                Using CurDoc As DocumentInfo = Document.GetDocumentInfo(iEntryID, RASession)
                    ms = New IO.MemoryStream
                    Dim CurExporter As DocumentExporter = New DocumentExporter()
                    CurExporter.ExportElecDoc(CurDoc, ms)
                End Using
            Catch ex As Exception
                sError = System.Reflection.MethodBase.GetCurrentMethod.Name & ": " & ex.Message
                ms = Nothing
            End Try
            Return ms
        End Function

        Private Sub Proc(ByVal LFEntryID As Integer)
            Using MemStrm As IO.MemoryStream = ExportEImage(LFEntryID)
                If MemStrm IsNot Nothing Then
                    If ImportImage(LFEntryID, MemStrm) Then
                        RemoveEDoc(LFEntryID)
                    End If
                End If
            End Using
        End Sub

    End Class
End Namespace

 

1 0
replied on October 4, 2017

This worked perfectly! Thank you so much Bert! You are a gentleman and a scholar, I truly appreciate all your help.

1 0
replied on September 22, 2017

Thanks Bert! I'll try this out later today and get back to you tomorrow. 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.