65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 Amyuni PDF Converter 将 PDF 打印到 Microsoft Azure 和 Google Cloud

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2012 年 3 月 20 日

CPOL

5分钟阅读

viewsIcon

24476

Google Cloud Storage 或 Microsoft Azure 等云服务是存储文档的绝佳存储库。通常希望将所有文档存储为标准的 PDF 格式。本文将介绍一种解决方案,用于自动化将 PDF 文档转换为云端并存储到云端。

Amazon S3、Google Cloud Storage、Google Docs、Dropbox、Box、Microsoft Azure 等云服务是存储文档的绝佳存储库。这些服务中的每一种都允许用户将文档存储卸载到云端。虽然任何类型的文档都可以存储在云端,但通常希望将所有文档存储为标准的 PDF 格式,并利用 PDF 带来的所有优势。将文档转换为 PDF 然后将其上传到云端通常是一个两步过程,需要手动操作,例如将文档打印到虚拟打印机并登录网站进行上传。

本文将介绍一种解决方案,用于在一个自动化过程中轻松地将文档转换为 PDF 以便存储在云端。

要求(服务器端)

一种提供 API 以允许开发人员自动化存储和检索文档任务的云服务。

仔细查看云服务,我们在决定使用哪种服务时发现了一些问题。

  • 实际上没有“免费”的评估服务。Google Cloud Storage、Amazon S3 和 Microsoft Azure 服务都需要信用卡信息进行评估。
  • Google Docs 提供免费评估,但有一个月的有效期。
  • 尽管 Dropbox 和 Box 服务都提供免费的开发者评估帐户,但这两种应用程序都要求客户或用户在文件传输发生之前先登录其网站。这会中断应用程序的工作流程。

在查看可用的云存储服务时,我们最终专注于 Google Docs(属于 Google Apps for Business)和 Microsoft Azure(Blob Storage Service)。

要求(客户端)

在查看云存储打印解决方案时,我们希望实现

  • 我们需要一个工具,在将所有用户文档发送到云端之前将其转换为 PDF。
  • 为了安全性和效率,我们需要直接传输或不存储在客户端 PC 上。
  • 我们需要一个易于实现和使用的解决方案。
  • 我们需要一个高性能的引擎,以免影响用户的工作流程。

Amyuni PDF Converter 满足了我们的所有要求。PDF Converter 是一个虚拟打印机驱动程序,可将发送到它的打印应用程序的输出转换为 PDF 格式。该打印机驱动程序已通过 Microsoft 认证,适用于所有 32 位和 64 位 Windows 版本,为开发人员提供了对打印过程的完全控制。运行本文所述示例应用程序所需的 Amyuni PDF Converter 产品可以从 http://www.amyuni.com/en/enduser/pdfconverterend 下载。

实现

Amyuni PDF Converter 打印机驱动程序 API 使开发人员能够拦截来自打印机驱动程序的数据流,并在其自定义应用程序中进行处理。开发人员可以选择将数据流本地存储、存储到网络驱动器,甚至存储到云端。

通过配置 PDF Converter 在打印作业期间调用自定义 DLL 来实现数据流的拦截。此自定义 DLL 将执行上传到云端的“工作”。

对于 Google 和 Azure 云服务,Amyuni PDF Converter 的安装和配置方式相同。首先在 PC 或服务器上安装 PDF Converter,然后使用我们的 API,开发人员需要配置它将打印机输出定向到我们的自定义 DLL。下面的代码片段说明了此过程。

//Declare object
CDIntfEx.CDIntfExClass PDF = new CDIntfEx.CDIntfExClass();

//Initialize printer
//This is printer name you used to install printer
PDF.DriverInit("Amyuni PDF Cloud Converter");

//This function needs to be called to enable printer
PDF.EnablePrinter(strLicenseTo, strActivationCode);

//The PDF Converter is configured to send data to a page processor DLL
PDF.FileNameOptionsEx = 0x2000000;

//This is the name of the DLL that will process datastream
PDF.SetPrinterParamStr("PageProcessor","PageProc.dll");
//Apply changes
PDF.SetDefaultConfig();

PageProc DLL 与 **acListener** 服务通信,并将从打印机驱动程序接收到的数据流发送给它。虽然可以编程该 DLL 将输出直接发送到云端,但我们选择使用中间的 **acListener** 服务,因为它为我们带来了以下优势:

  • 在数据完全上传到服务器之前,控制权会快速返回给用户。
  • 监听器服务可以只对用户进行一次身份验证,而 DLL 则需要在每次加载时进行身份验证。
  • 通过在上传文档之前使用 Amyuni PDF Creator.Net 查看器,可以在监听器中轻松实现 PDF 文档的预览。PDF Creator.Net 可从以下 URL 下载。
    http://www.amyuni.com/en/developer/pdfcreator

上传到 Google Docs

Google Docs 提供了以下优势:

  • 免费 30 天的企业帐户访问权限。
  • 极其庞大的用户群。
  • Google Docs 为用户提供了文档的可视化表示。
  • 在线文档,支持实时协作。从 PC 打印,使文档可供其他用户或从其他 PC 访问。

要使用 Google Docs(Google Documents List API),您需要注册一个 Google Apps for Business 帐户。评估此服务不需要信用卡,但需要您在网站的索引页面中添加一个 HTML 标签进行身份验证。

Google Docs 与其他服务的区别在于,它为用户提供了 Web 界面来查看其文档并与其他用户协作。

所有上传到 Google Docs 的操作都发生在 Upload () 方法中。

下面的代码片段说明了

        //create menory stream of date generated by
        //the PDF Converter printer.
        MemoryStream data = _document.GetData();

下面的代码片段(位于 acListener 服务中)处理将数据流上传到云端。

public void Upload()
{
    //////////////////////////////////////////////////////////////////
    /*The DocumentsService class represents a client connection 
     * (with authentication) to the Google Docs web service.

   Setting your application's name 
   (in the form companyName-applicationName-versionID)*/
    //////////////////////////////////////////////////////////////////
   DocumentsService service = new 
            DocumentsService("AmyuniTech-AmyuniCloudPrinterApp-v1.0");


    //RequestFactory to create a request for the particular query
    //A request factory to generate an authorization header suitable 
    //for use with OAuth user authenticate through Google's servers.
    GDataGAuthRequestFactory reqFactory = 
                        (GDataGAuthRequestFactory)service.RequestFactory;

    //indicates if the connection should be kept alive
    reqFactory.KeepAlive = false;
    //Use v3 of the API
    reqFactory.ProtocolMajor = 3;
    

    service.setUserCredentials(_googleUsername , _googleUserPassword);


    DocumentEntry entry = null;

    //create menory stream of date generated by
    //the PDF Converter printer.
    MemoryStream data = _document.GetData();

    //rewind the data
    data.Seek(0, SeekOrigin.Begin);
    try
    {
        //Tell Google that you are going to be sending a PDF document.
//_googleDocumentName is the name of the PDF file that will appear in Google docs.
        String contentType = (String)DocumentsService.DocumentTypes["PDF"];
        entry = service.Insert(new Uri(DocumentsListQuery.documentsBaseUri),
                                data,
                                contentType,
                                _googleDocumentName) as DocumentEntry;
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
    }
    finally
    {
    }
    _document.ReleaseStream();
    data.Close();
}

上传到 Microsoft Azure Blob Storage

上传到 Microsoft Azure Blob Storage 服务的过程类似。

Microsoft Blob Storage Service 对开发人员的主要优势在于其丰富的 .NET 文档。

Microsoft Azure Blob Storage Service (MABSS) 使用容器和 blob 的概念来模拟文件系统。

Microsoft 将容器定义为“一个容器提供了一组 blob 的分组。所有 blob 都必须位于容器中。一个帐户可以包含无限数量的容器。一个容器可以存储无限数量的 blob。”

Microsoft 将 blob 定义为“任何类型和大小的文件”。

在下面的代码片段中,Amyuni PDF Converter 将数据流传递给 Blob 对象上的 UploadFromStream() 方法,以将文件上传到云端。

// to blob on cloud
blob.UploadFromStream(data);

/// <summary>
/// This method uses a acPbpDocument object
/// It loads this object to the cloud.
/// </summary>
public void Upload()
{              
    //create memory stream of data generated by
    //the PDF Converter printer.
    MemoryStream data = _document.GetData();
 
    //rewind the data
    data.Seek(0, SeekOrigin.Begin);
    try
    {
 
        CloudStorageAccount storageAccount = 
            CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageAccountConnectionString"]);
 
        // Create the blob client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
 
        // Retrieve reference to a previously created container
        //This is like a directory
        CloudBlobContainer container = blobClient.GetContainerReference("pdfdocuments");
 
        // Create the container if it doesn't already exist
        container.CreateIfNotExist();
 
        // Retrieve reference to a blob - this is where PDF document is going to be upload
        //- like filename
        CloudBlob blob = container.GetBlobReference(_pdfDocumentName + ".pdf");
 
        // blob on cloud
        blob.UploadFromStream(data);
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
    }
 
    _document.ReleaseStream();
    data.Close();
           
}

可以通过填写我们的联系表格 http://www.amyuni.com/en/company/contactform/ 向 Jose 申请用于 Google Docs 和 Azure 的 acListener 服务。

© . All rights reserved.