【问题标题】:PDFTron Convert from HTML to PDF on WinRTPDFTron 在 WinRT 上从 HTML 转换为 PDF
【发布时间】:2013-11-09 03:19:49
【问题描述】:

我正在使用 WinJS 编写一个 Windows 8 应用商店应用程序。我的应用程序需要生成带有文本和图形的 PDF。我的印象是 PDFtron 可以将 HTML 转换为 PDF,但 App Store 应用程序似乎并非如此。这是真的吗?

前端使用 WinJS/HTML 和 Telerik Radcharts 以 SVG 呈现图形。然后,我将 DOM 作为 HTML 文件传送到磁盘。它很好地显示了图表和数字。我想将 HTML 转换为 PDF 以保留样式和内容。

WinRT 版本不附带 HTML2PDF 程序集或 .Convert() 方法。是不是在别的地方?我搜索了文档、示例和网络。

【问题讨论】:

    标签: windows-runtime pdf-generation winjs


    【解决方案1】:

    WinRT 上的 PDFTron 的 PDFNet SDK 不支持 HTML 到 PDF 的转换(在 6.2 版中)。

    这是我从 PDFTron 支持部门收到的关于这个问题的回复:

    虽然 WinRT 上的 PDFNet SDK 自身无法从 HTML 转换 对于 PDF,Windows 桌面上的 PDFNet SDK 可以做到这一点。你可以找到 HTML 到 PDF 转换的示例代码 http://www.pdftron.com/pdfnet/samplecode.html#HTML2PDF.

    我们的一些客户将 HTML 发送到他们的服务器,其中 PDFNet 可以将 HTML 转换为 PDF。请注意,在 Windows 桌面有很多转换选项,包括转换 Office 转换为 PDF 并将任何可打印的文档格式转换为 PDF。

    【讨论】:

      【解决方案2】:

      EVO 实现了以下solution to convert HTML to PDF in WinRT and Windows Store Applications。您可以在该页面中找到完整的代码示例。

      代码示例的副本是:

      private async void buttonConvertUrlToPdf_Click(object sender, RoutedEventArgs e)
      {
          // If another conversion is in progress then ignore current request
          bool ignoreRequest = false;
          lock(pendingConversionSync)
          {
              if (pendingConversion)
                  ignoreRequest = true;
              else
              {
                  msgUrlToPdfInProgress.Visibility = Windows.UI.Xaml.Visibility.Visible;
                  pendingConversion = true;
              }
          }
      
          if (ignoreRequest)
              return;
      
          try
          {
              String serverIP = textBoxServerIP.Text;
              uint port = uint.Parse(textBoxServerPort.Text);
      
              HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter(serverIP, port);
      
              // set service password if necessary
              if (textBoxServicePassword.Text.Length > 0)
                  htmlToPdfConverter.ServicePassword = textBoxServicePassword.Text;
      
              // set HTML viewer width
              htmlToPdfConverter.HtmlViewerWidth = int.Parse(textBoxHtmlViewerWidth.Text);
      
              // set HTML viewer height if necessary
              if (textBoxHtmlViewerHeight.Text.Length > 0)
                  htmlToPdfConverter.HtmlViewerHeight = int.Parse(textBoxHtmlViewerHeight.Text);
      
              // set navigation timeout
              htmlToPdfConverter.NavigationTimeout = int.Parse(textBoxHtmlViewerWidth.Text);
      
              // set conversion delay if necessary
              if (textBoxConversionDelay.Text.Length > 0)
                  htmlToPdfConverter.ConversionDelay = int.Parse(textBoxConversionDelay.Text);
      
              // set PDF page size
              htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize();
      
              // set PDF page orientation
              htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation();
      
              // set margins
              htmlToPdfConverter.PdfDocumentOptions.LeftMargin = int.Parse(textBoxLeftMargin.Text);
              htmlToPdfConverter.PdfDocumentOptions.RightMargin = int.Parse(textBoxRightMargin.Text);
              htmlToPdfConverter.PdfDocumentOptions.TopMargin = int.Parse(textBoxTopMargin.Text);
              htmlToPdfConverter.PdfDocumentOptions.BottomMargin = int.Parse(textBoxBottomMargin.Text);
      
              // add header
              if (checkBoxAddHeader.IsChecked != null && (bool)checkBoxAddHeader.IsChecked)
              {
                  htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true;
                  DrawHeader(htmlToPdfConverter, true);
              }
      
              // add footer
              if (checkBoxAddFooter.IsChecked != null && (bool)checkBoxAddFooter.IsChecked)
              {
                  htmlToPdfConverter.PdfDocumentOptions.ShowFooter = true;
                  DrawFooter(htmlToPdfConverter, true, true);
              }
      
              string urlToConvert = textBoxUrl.Text;
              string errorMessage = null;
      
              // Convert the HTML page from give URL to PDF in a buffer
              byte[] pdfBytes = await Task.Run<byte[]>(() =>
              {
                  byte[] resultBytes = null;
                  try
                  {
                      resultBytes = htmlToPdfConverter.ConvertUrl(urlToConvert);
                  }
                  catch (Exception ex)
                  {
                      errorMessage = String.Format("Conversion failed. {0}", ex.Message);
                      return null;
                  }
      
                  return resultBytes;
              });
      
              if (pdfBytes == null)
              {
                  MessageDialog errorMessageDialog = new MessageDialog(errorMessage, "Conversion failed");
                  await errorMessageDialog.ShowAsync();
                  return;
              }
      
              // Save the PDF in a file
              Windows.Storage.StorageFolder installedLocation = Windows.Storage.ApplicationData.Current.LocalFolder;
              StorageFile outStorageFile = installedLocation.CreateFileAsync("EvoHtmlToPdf.pdf", CreationCollisionOption.ReplaceExisting).AsTask().Result;
              FileIO.WriteBytesAsync(outStorageFile, pdfBytes).AsTask().Wait();
      
              // Open the file in a PDF viewer
              await Windows.System.Launcher.LaunchFileAsync(outStorageFile);
          }
          finally
          {
              lock (pendingConversionSync)
              {
                  msgUrlToPdfInProgress.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                  pendingConversion = false;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-03-07
        • 1970-01-01
        • 2012-11-21
        • 1970-01-01
        相关资源
        最近更新 更多