Search code examples
pdfazurepdfnet

PDFNet SDK Convert files on Azure storage


I have a web app that needs to convert PDFs to XODs (PDFTron’s format to display documents in their WebViewer). My Web App is hosted on Azure, the PDFs are on Azure Storage. We would like to go along with the on-premises conversion via PDFNet SDK (http://www.pdftron.com/webviewer/getstarted.html, see “Choosing a deployment model for conversions), my code so far is the following:

WebRequest req = HttpWebRequest.Create("url of PDF on Azure Storage here");                         
using (Stream stream = req.GetResponse().GetResponseStream())
{
    PDFDoc pdfdoc = new PDFDoc(stream);
    var converted = pdftron.PDF.Convert.ToXod(pdfdoc);
    //pdfdoc.Save(stream, pdftron.SDF.SDFDoc.SaveOptions.e_linearized); //not clear to me
}

My approach here is to create a stream from the file on azure storage and convert that stream to XOD. I still don’t know if I should call “Save” and in that case where the file would be saved. My questions are:

  1. Since everything runs on the cloud does it make sense to use the CloudAPI instead of the self-hosted solution or does it not make any difference?
  2. In both cases, where is the converted file stored (since I am getting it from the Azure storage and not from a local server), since I would then need to move it to my azure storage account. Does the file get saved locally (meaning on the web/worker role which is processing it) and therefore needs to be moved to the storage?

Here (http://www.pdftron.com/pdfnet/samplecode.html) there are conversion code samples but they all use files on local machine, which would not be my case.


Solution

  • After some extra research I found out I can get and set the Stream of the source and destination files (even if the destination file does not exist yet) directly on Azure without downloading the file. The resulting code is then something like

    using (var sourceStream = sourceBlob.OpenRead())
    {
        var destinationContainer = BlobClient.GetContainerReference(projectKey);
        var destinationBlob = destinationContainer.GetBlockBlobReference(xodName);
        using (var destinationStream = destinationBlob.OpenWrite())
        {
            var pdfDoc = new PDFDoc(sourceStream);
            pdftron.PDF.Convert.ToXod(pdfDoc);
            pdfDoc.Save(destinationStream, pdftron.SDF.SDFDoc.SaveOptions.e_linearized);
        });
    });