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

Question

Question

Export pdf using SDK script over HTTP Web Request

asked on February 12, 2020 Show version history

I want to write a WF SDK Script where I can convert tiff files into pdf, and instead of putting them into a network drive I want to be able to send it across an HTTP Web Request (the whole file) this will be a POST call.

Right now, I have a WF which sends the metadata over the HTTP Web Request, but now I want to send the whole file.

 

Thanks.

 

Edit: a word

0 0

Replies

replied on February 12, 2020

You would first export the Document to a memorystream and then convert the ms to a Base64 String.

See Bills post in SDK DocumentExported to MemoryStream PDF question for sample of exporting to memorystream.

2 0
replied on February 12, 2020 Show version history

Hey Bert, please help me understand this. I'm sort of lost, I get was Bill is saying, but if you can explain it a bit further. 

I also make a WF as a reference. 

I'm thinking about these steps below:

1. Take tiff files and convent them to pdf

2. Take those pdf document to memorystream and then convert the ms to a Base64String 

3. Then send the string out to the HTTP Web Request Post call 

     a. what would be in the content body in the POST call?

4. All these would be a part of an LF WF 

 

Edit: added few words.

Capture.PNG
Capture.PNG (27.37 KB)
0 0
replied on February 12, 2020 Show version history

In your SDK Script, Once you have the Base64 String, create a token with it

SetTokenValue("TokenName", Base64StringObject)

Then you use the token created in the SDK script to send the data.

1 0
replied on February 13, 2020

Bret,

If I modify your example from Workflow to Export Document to Windows Folder

Next, take Bill's code from above about SDK DocumentExported to MemoryStream PDF question 

and then add 

SetTokenValue("TokenName", Base64StringObject)

all that in a Workflow to make the HTTP WebRequest, would that be possible?

0 0
replied on February 13, 2020

The code in my example Workflow to Export Document to Windows Folder exports to a Windows folder.  You do not want to write the file to disk, instead you want to write the file to a MemoryStream.  You can edit my example code to export to MemoryStream, and SDK DocumentExported to MemoryStream PDF question shows how to export to MemoryStream.

So in your SDK Script 

  1. Export to MemoryStream
  2. Convert MemoryStream to Base64String
  3. Create Token with Base64String

Then in your Web Post, you use the Token from the SDK Script to pass the data to your Web Service.

0 0
replied on February 13, 2020 Show version history

I modify one of the methods and I have been stuck on it for a long time if you can please suggest some input

 

Some of the errors I'm getting are:

The name 'LF_DocExporter' does not exist in the current context 

The name 'LF_DocInfo' does not exist in the current context
Cannot implicitly convert type 'string' to 'byte[]'  
The name 'Base64StringObject' does not exist in the current context   

        private void ExportTextDocument(DocumentInfo LFDoc){
            try{
                String sTxt = null;
                System.IO.MemoryStream pdf = new System.IO.MemoryStream();
                byte[] ExportedByte = {0};
                Array.Resize<byte>(ref ExportedByte, 0);

                LF_DocExporter.ExportPdf(FileNameChange(LFDoc.Name), LF_DocInfo, LF_DocInfo.AllPages, PdfExportOptions.None, pdf);

                ExportedByte = Convert.ToBase64String(pdf.ToArray());
                SetTokenValue("ExportTextDocument", Base64StringObject);
                //String exportPath= System.IO.Path.Combine(sExportFolder, FileNameChange(LFDoc.Name) + ".txt");
                
                using (PageInfoReader LF_PageInfos = LFDoc.GetPageInfos()){
                    foreach (PageInfo PI in LF_PageInfos){
                        if (PI.HasText){
                            using (System.IO.StreamReader reader = PI.ReadTextPagePart()){
                                if (String.IsNullOrEmpty(sTxt)){
                                    sTxt = reader.ReadToEnd();
                                }else{
                                    sTxt = sTxt + Environment.NewLine + reader.ReadToEnd();
                                }
                            }
                        }
                    }
                }
            } catch (Exception ex){
                WorkflowApi.TrackError(ex.Message);
            }
        }

 

0 0
replied on February 14, 2020 Show version history

You have to define/create objects before you can use them.  All of the errors about the object does not exist is because you are not defining it.

Here is code that, like the Workflow to Export Document to Windows Folder code, will export the BoundEntry to a Base64String token.  Add the reference to the DocumentServices as shown in Workflow to Export Document to Windows Folder and then this code should work.

NOTE:  You will have to build in logic to ensure that you only export images to PDF or PDF electronic documents.  Otherwise you will have to build logic to tell the Web Service what type of document you are sending.

        protected override void Execute()
        {
            // Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            String base64String = null;
            try
            {
                // Get Setting for Exporting Image Documents
                String sImageToPDF = (String)GetTokenValue("ExportImagesAsPDF");
                // Only Export Documents
                if (IsEntryDocument(BoundEntryInfo))
                {
                    // Get DocumentInfo object
                    using (DocumentInfo doc = (DocumentInfo)BoundEntryInfo)
                    {
                        if (IsElectronicDocument(doc)) // Check if it is an electronic document
                        {
                            // Export the electronic Portion of the document
                            base64String = ExportElectronicToBase64(doc);
                        }
                        else if (HasImagePages(doc)) // Check for Image pages
                        {
                            if (sImageToPDF.ToLower() == "true")
                            {
                                // Export images to PDF
                                base64String = ExportImageDocumentToPDFToBase64(doc);
                            }
                            else
                            {
                                // Export images to TIFF
                                base64String = ExportImageDocumentToBase64(doc);
                            }
                        }
                        else
                        {
                            if (HasText(doc)) // if it has Text
                            {
                                // Export text file
                                base64String = ExportTextDocumentToBase64(doc);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                base64String = null;
            }
            SetTokenValue("base64String", base64String);
        }

        private String ExportTextDocumentToBase64(DocumentInfo LFDoc)
        {
            String base64String = null;
            try
            {
                String sTxt = null;
                using (PageInfoReader LF_PageInfos = LFDoc.GetPageInfos())
                {
                    foreach (PageInfo PI in LF_PageInfos)
                    {
                        if (PI.HasText)
                        {
                            using (System.IO.StreamReader reader = PI.ReadTextPagePart())
                            {
                                if (String.IsNullOrEmpty(sTxt))
                                {
                                    sTxt = reader.ReadToEnd();
                                }
                                else
                                {
                                    sTxt = sTxt + Environment.NewLine + reader.ReadToEnd();
                                }
                            }
                        }
                    }
                }
                using (System.IO.MemoryStream  MS = new System.IO.MemoryStream())
                {
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(MS))
                    {
                        file.Write(sTxt);
                    }
                    base64String = Convert.ToBase64String(MS.ToArray());
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                base64String = null;
            }
            return base64String;
        }

        private String ExportImageDocumentToBase64(DocumentInfo LFDoc)
        {
            String base64String = null;
            try
            {
                DocumentExporter docExporter = new DocumentExporter();
                docExporter.PageFormat = DocumentPageFormat.Tiff;
                using (System.IO.MemoryStream MS = new System.IO.MemoryStream())
                {
                    docExporter.ExportPages(LFDoc, GetImagePageSet(LFDoc), MS);
                    base64String = Convert.ToBase64String(MS.ToArray());
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                base64String = null;
            }
            return base64String;
        }

        private String ExportImageDocumentToPDFToBase64(DocumentInfo LFDoc)
        {
            String base64String = null;
            try
            {

                DocumentExporter docExporter = new DocumentExporter();
                using (System.IO.MemoryStream MS = new System.IO.MemoryStream())
                {
                    docExporter.ExportPdf(LFDoc, GetImagePageSet(LFDoc), PdfExportOptions.None, MS);
                    base64String = Convert.ToBase64String(MS.ToArray());
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                base64String = null;
            }
            return base64String;
        }

        private String ExportElectronicToBase64(DocumentInfo LFDoc)
        {
            String base64String = null;
            try
            {
                DocumentExporter docExporter = new DocumentExporter();
                using (System.IO.MemoryStream MS = new System.IO.MemoryStream())
                {
                    docExporter.ExportElecDoc(LFDoc, MS);
                    base64String = Convert.ToBase64String(MS.ToArray());
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                base64String = null;
            }
            return base64String;
        }

        private PageSet GetImagePageSet(DocumentInfo LFDoc)
        {
            PageSet psReturn = new PageSet();
            try
            {
                using (PageInfoReader LF_PageInfos = (PageInfoReader)LFDoc.GetPageInfos())
                {
                    foreach (PageInfo PI in LF_PageInfos)
                    {
                        using (LaserficheReadStream lrs = PI.ReadPagePart(new PagePart()))
                        {
                            if (lrs.Length > 0)
                            {
                                psReturn.AddPage(PI.PageNumber);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                psReturn = new PageSet();
            }
            return psReturn;
        }

        private Boolean HasText(DocumentInfo LFDoc)
        {
            Boolean bReturn = false;
            try
            {
                using (PageInfoReader LF_PageInfos = LFDoc.GetPageInfos())
                {
                    foreach (PageInfo PI in LF_PageInfos)
                    {
                        if (PI.HasText)
                        {
                            bReturn = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                bReturn = false;
            }
            return bReturn;
        }

        private Boolean HasImagePages(DocumentInfo LFDoc)
        {
            Boolean bReturn = false;
            try
            {
                using (PageInfoReader LF_PageInfos = LFDoc.GetPageInfos())
                {
                    foreach (PageInfo PI in LF_PageInfos)
                    {
                        using (LaserficheReadStream lrs = PI.ReadPagePart(new PagePart()))
                        {
                            if (lrs.Length > 0)
                            {
                                bReturn = true;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                bReturn = false;
            }
            return bReturn;
        }

        private Boolean IsElectronicDocument(DocumentInfo LFDoc)
        {
            Boolean bReturn = false;
            try
            {
                bReturn = LFDoc.IsElectronicDocument;
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                bReturn = false;
            }
            return bReturn;
        }

        private Boolean IsEntryDocument(EntryInfo LFEnt)
        {
            Boolean bReturn = false;
            try
            {
                if (LFEnt.EntryType == EntryType.Document)
                {
                    bReturn = true;
                }
                else
                {
                    bReturn = false;
                }
            }
            catch (Exception ex)
            {
                WorkflowApi.TrackError(ex.Message);
                bReturn = false;
            }
            return bReturn;
        }

 

2 0
replied on June 2, 2020 Show version history

It's not the same use case, but we had a need recently to export either PDF files or Office Documents to disk.  I found it to be quite simple:

 


ADD THIS: 

Imports Laserfiche.DocumentServices

Namespace WorkflowActivity.Scripting.ScriptExportOfficeDoc
    Public Class Script1
        Inherits RAScriptClass104
        Protected Overrides Sub Execute()

            Dim LF_DocExporter As New DocumentExporter()
            Dim sExportPath as String = GetTokenValue ("ExportPath")
            Dim LF_DocInfo as DocumentInfo = Me.BoundEntryInfo
            Dim sExt as String = LF_DocInfo.Extension
            'Entry ID allows us to ID the file and tie the indexes back together.
            Dim sDocName as String = LF_DocInfo.Id & "_" & LF_DocInfo.Name & "." & sExt

            'Set up the export. Document name, extension
           LF_DocExporter.ExportElecDoc (LF_DocInfo, sExportPath & sDocName)

           'For Testing 
           'SetTokenValue("Extension", sExportPath & sDocName)

       End Sub
    End Class
End Namespace

 

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

Sign in to reply to this post.