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

Question

Question

SDK DocumentExported to MemoryStream PDF question.

SDK
asked on April 21, 2016 Show version history

I've got a customer who is trying to create a memorystream of a multipage document to send to another process as a PDF. 

 

var pdf = new MemoryStream();

            using (var lfSession = new Session())
            {
                var lfRepository = new RepositoryRegistration(
                    LaserficheConfiguration.ServerName,
                    LaserficheConfiguration.RepositoryName);

             
                    lfSession.LogIn(lfRepository);
           

                //Verify if id is a valid Entry. This is here because
                //GetDocumentInfo throws an error when EntryID is not found rather than returning a null.
                if (Entry.TryGetEntryInfo(id, lfSession) != null)
                {
                    var doc = Document.GetDocumentInfo(id, lfSession).GetLatestVersion();
                    DocumentExporter docExp = new DocumentExporter();
                    docExp.ExportPdf(doc, ((DocumentInfo)doc).AllPages, PdfExportOptions.None, pdf);
                }
            }

            return File(pdf.ToArray(), "application/pdf", $"{id}.pdf");

 

So from reading the SDK documentation, it looks like documentexporter.exporter does not support streaming multipage documents in the PDF format. 

From the sdk for that method:

Remarks 

The Export method cannot export multi-page documents to byte arrays, so this method will fail if the specified format is PDF. 

 

 

So does anyone have any good solutions for this? Do we need to use third party tools like iTextSharp to create the pdf from a tiff stream? Or can you send multiple pages one at a time to the memory stream array and have them show up as a multipage PDF?

0 0

Replies

replied on April 21, 2016

Export to a temporary file and read that file into a MemoryStream:

string pdfFilePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

using (FileStream fs = File.Create(pdfFilePath))
    docExp.ExportPdf(doc, doc.AllPages, PdfExportOptions.None, fs);

MemoryStream ms = new MemoryStream(File.ReadAllBytes(pdfFilePath));
File.Delete(pdfFilePath);

 

2 0
replied on April 21, 2016

yeah, I guess that would work. Maybe I was just overthinking it trying to figure out a way to keep it all in memory. :)

 

0 0
replied on April 27, 2016 Show version history

It might just be a conversion problem.  This post made me nervous as we are doing the exact same thing in a web service.  Fortunately we are not having this problem, with just some slight differences in execution:

 

        'This is similar to what you have
        Dim pdf As New System.IO.MemoryStream()

        'The next two lines are additional
        Dim ExportedByte As Byte() = {0}
        Array.Resize(Of Byte)(ExportedByte, 0)

        'The export call is the same
        LF_DocExporter.ExportPdf(LF_DocInfo, LF_DocInfo.AllPages, PdfExportOptions.None, pdf)

        'Here we convert the memory stream to the byte array, and return just a single object.
        ExportedByte = pdf.ToArray

        Return ExportedByte 

 

1 0
replied on April 27, 2016

Thank you Bill!  I'll relay that to the customer for them to try. I know the temporary file issue was a fix, but they wanted to keep it diskless for speed if possible. 

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

Sign in to reply to this post.