生成 Winnovative PDF 为一定的宽度/高度像素大小

发布于 2024-12-06 09:31:27 字数 471 浏览 1 评论 0原文

我希望使用 Winnovative PDF 转换器从 HTML 生成 PDF 文档

我希望将 PDF 转换为精确的 842 x 595 像素

我已经尝试过:

pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A5

但这不合适。所以我尝试过:

    pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Custom
    pdfConverter.PdfDocumentOptions.CustomPdfPageSize = New SizeF(842, 595)

但是这也不起作用,因为我认为尺寸是以与像素不同的格式测量的?

如何生成精确为 842 x 595 像素的 PDF,使其与我的 HTML 内容匹配?

I'm looking to generate a PDF document from HTML using the Winnovative PDF Converter

I wish to convert the PDF to exactly 842 x 595 pixels

I've tried doing:

pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A5

But this doesn't fit right. So I tried:

    pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.Custom
    pdfConverter.PdfDocumentOptions.CustomPdfPageSize = New SizeF(842, 595)

However this doesn't work right either as I think the size is measured in a different format to pixels?

How can I generate a PDF at exactly 842 x 595 pixels so that it matches my HTML content?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿与i 2024-12-13 09:31:27

将 HTML 内容渲染为 PDF 时涉及两件事。 PDF 页面大小和转换器内部 HTML 查看器宽度。启用 FitWidth 后,可以缩小 HTML 内容以适合 PDF 页面宽度。

PDF 页面大小以磅表示(1 磅为 1/72 英寸),内部 HTML 查看器窗口大小以像素表示(i 像素为 1/96 英寸)。

PDF 页面 A4 肖像尺寸为 595 x 842 点,与 595 点对应的 HTML 查看器宽度为 595 x 96 / 72 = 793 像素。

所以转换器的设置是:

pdfConverter.HtmlViewerWidth = 793
pdfConverter.PdfDocumentOptions.PdfPageSize = new PdfPageSize(595,842)

您可以在 控制 PDF 页面演示中的 HTML 缩放。以下是该演示中相关代码的副本:

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

    // Html Viewer Options

    // Set HTML Viewer width in pixels which is the equivalent in converter of the browser window width
    // This is a preferred width of the browser but the actual HTML content width can be larger in case the HTML page 
    // cannot be entirely displayed in the given viewer width
    // This property gives the size of the HTML content which can be further scaled to fit the PDF page based on selected options
    // The HTML content size is in pixels and the PDF page size is in points (1 point = 1/72 inches)
    // The converter is using a 96 DPI resolution to transform pixels to points with the following formula: Points = Pixels/96 * 72            
    htmlToPdfConverter.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text);

    // Set HTML viewer height in pixels to convert the top part of a HTML page 
    // Leave it not set to convert the entire HTML
    if (htmlViewerHeightTextBox.Text.Length > 0)
        htmlToPdfConverter.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text);

    // Set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels
    // If this option is false then the actual HTML content width can be larger than HtmlViewerWidth pixels in case the HTML page 
    // cannot be entirely displayed in the given viewer width
    // By default this option is false and the HTML content is not clipped
    htmlToPdfConverter.ClipHtmlView = clipContentCheckBox.Checked;

    // PDF Page Options

    // Set PDF page size which can be a predefined size like A4 or a custom size in points 
    // Leave it not set to have a default A4 PDF page
    htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize();

    // Set PDF page orientation to Portrait or Landscape
    // Leave it not set to have a default Portrait orientation for PDF page
    htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation();

    // Set PDF page margins in points or leave them not set to have a PDF page without margins
    htmlToPdfConverter.PdfDocumentOptions.LeftMargin = float.Parse(leftMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.RightMargin = float.Parse(rightMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.TopMargin = float.Parse(topMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.BottomMargin = float.Parse(bottomMarginTextBox.Text);

    // HTML Content Destination and Spacing Options

    // Set HTML content destination in PDF page
    if (xLocationTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.X = float.Parse(xLocationTextBox.Text);
    if (yLocationTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Y = float.Parse(yLocationTextBox.Text);
    if (contentWidthTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Width = float.Parse(contentWidthTextBox.Text);
    if (contentHeightTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Height = float.Parse(contentHeightTextBox.Text);

    // Set HTML content top and bottom spacing or leave them not set to have no spacing for the HTML content
    htmlToPdfConverter.PdfDocumentOptions.TopSpacing = float.Parse(topSpacingTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.BottomSpacing = float.Parse(bottomSpacingTextBox.Text);

    // Scaling Options

    // Use this option to fit the HTML content width in PDF page width
    // By default this property is true and the HTML content can be resized to fit the PDF page width
    htmlToPdfConverter.PdfDocumentOptions.FitWidth = fitWidthCheckBox.Checked;

    // Use this option to enable the HTML content stretching when its width is smaller than PDF page width
    // This property has effect only when FitWidth option is true
    // By default this property is false and the HTML content is not stretched
    htmlToPdfConverter.PdfDocumentOptions.StretchToFit = stretchCheckBox.Checked;

    // Use this option to automatically dimension the PDF page to display the HTML content unscaled
    // This property has effect only when the FitWidth property is false
    // By default this property is true and the PDF page is automatically dimensioned when FitWidth is false
    htmlToPdfConverter.PdfDocumentOptions.AutoSizePdfPage = autoSizeCheckBox.Checked;

    // Use this option to fit the HTML content height in PDF page height
    // If both FitWidth and FitHeight are true then the HTML content will resized if necessary to fit both width and height 
    // preserving the aspect ratio at the same time
    // By default this property is false and the HTML content is not resized to fit the PDF page height
    htmlToPdfConverter.PdfDocumentOptions.FitHeight = fitHeightCheckBox.Checked;

    // Use this option to render the whole HTML content into a single PDF page
    // The PDF page size is limited to 14400 points
    // By default this property is false
    htmlToPdfConverter.PdfDocumentOptions.SinglePage = singlePageCheckBox.Checked;

    string url = urlTextBox.Text;

    // Convert the HTML page to a PDF document using the scaling options
    byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);

    // Send the PDF as response to browser

    // Set response content type
    Response.AddHeader("Content-Type", "application/pdf");

    // Instruct the browser to open the PDF file as an attachment or inline
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename=HTML_Content_Scaling.pdf; size={0}", outPdfBuffer.Length.ToString()));

    // Write the PDF document buffer to HTTP response
    Response.BinaryWrite(outPdfBuffer);

    // End the HTTP response and stop the current page processing
    Response.End();
}

There are 2 things involved when rendering the HTML content to PDF. The PDF page size and the converter internal HTML viewer width. When FitWidth is enabled the HTML content can be scaled down to fit the PDF page width.

The PDF page size is expressed in points (1 point is 1/72 inch) and the internal HTML viewer window size is expressed in pixels (i pixel is 1/96 inch).

The PDF page A4 potrait size is 595 x 842 points and the HTML viewer width corresponding to the 595 points is 595 x 96 / 72 = 793 pixels.

So the settings of the converter are:

pdfConverter.HtmlViewerWidth = 793
pdfConverter.PdfDocumentOptions.PdfPageSize = new PdfPageSize(595,842)

You can find a description of all the HTML scaling and fitting options and sample code in the Control HTML Scaling in PDF Page demo. Below is a copy of the relevant code from that demo:

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // Create a HTML to PDF converter object with default settings
    HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

    // Set license key received after purchase to use the converter in licensed mode
    // Leave it not set to use the converter in demo mode
    htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

    // Html Viewer Options

    // Set HTML Viewer width in pixels which is the equivalent in converter of the browser window width
    // This is a preferred width of the browser but the actual HTML content width can be larger in case the HTML page 
    // cannot be entirely displayed in the given viewer width
    // This property gives the size of the HTML content which can be further scaled to fit the PDF page based on selected options
    // The HTML content size is in pixels and the PDF page size is in points (1 point = 1/72 inches)
    // The converter is using a 96 DPI resolution to transform pixels to points with the following formula: Points = Pixels/96 * 72            
    htmlToPdfConverter.HtmlViewerWidth = int.Parse(htmlViewerWidthTextBox.Text);

    // Set HTML viewer height in pixels to convert the top part of a HTML page 
    // Leave it not set to convert the entire HTML
    if (htmlViewerHeightTextBox.Text.Length > 0)
        htmlToPdfConverter.HtmlViewerHeight = int.Parse(htmlViewerHeightTextBox.Text);

    // Set the HTML content clipping option to force the HTML content width to be exactly HtmlViewerWidth pixels
    // If this option is false then the actual HTML content width can be larger than HtmlViewerWidth pixels in case the HTML page 
    // cannot be entirely displayed in the given viewer width
    // By default this option is false and the HTML content is not clipped
    htmlToPdfConverter.ClipHtmlView = clipContentCheckBox.Checked;

    // PDF Page Options

    // Set PDF page size which can be a predefined size like A4 or a custom size in points 
    // Leave it not set to have a default A4 PDF page
    htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = SelectedPdfPageSize();

    // Set PDF page orientation to Portrait or Landscape
    // Leave it not set to have a default Portrait orientation for PDF page
    htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = SelectedPdfPageOrientation();

    // Set PDF page margins in points or leave them not set to have a PDF page without margins
    htmlToPdfConverter.PdfDocumentOptions.LeftMargin = float.Parse(leftMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.RightMargin = float.Parse(rightMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.TopMargin = float.Parse(topMarginTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.BottomMargin = float.Parse(bottomMarginTextBox.Text);

    // HTML Content Destination and Spacing Options

    // Set HTML content destination in PDF page
    if (xLocationTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.X = float.Parse(xLocationTextBox.Text);
    if (yLocationTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Y = float.Parse(yLocationTextBox.Text);
    if (contentWidthTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Width = float.Parse(contentWidthTextBox.Text);
    if (contentHeightTextBox.Text.Length > 0)
        htmlToPdfConverter.PdfDocumentOptions.Height = float.Parse(contentHeightTextBox.Text);

    // Set HTML content top and bottom spacing or leave them not set to have no spacing for the HTML content
    htmlToPdfConverter.PdfDocumentOptions.TopSpacing = float.Parse(topSpacingTextBox.Text);
    htmlToPdfConverter.PdfDocumentOptions.BottomSpacing = float.Parse(bottomSpacingTextBox.Text);

    // Scaling Options

    // Use this option to fit the HTML content width in PDF page width
    // By default this property is true and the HTML content can be resized to fit the PDF page width
    htmlToPdfConverter.PdfDocumentOptions.FitWidth = fitWidthCheckBox.Checked;

    // Use this option to enable the HTML content stretching when its width is smaller than PDF page width
    // This property has effect only when FitWidth option is true
    // By default this property is false and the HTML content is not stretched
    htmlToPdfConverter.PdfDocumentOptions.StretchToFit = stretchCheckBox.Checked;

    // Use this option to automatically dimension the PDF page to display the HTML content unscaled
    // This property has effect only when the FitWidth property is false
    // By default this property is true and the PDF page is automatically dimensioned when FitWidth is false
    htmlToPdfConverter.PdfDocumentOptions.AutoSizePdfPage = autoSizeCheckBox.Checked;

    // Use this option to fit the HTML content height in PDF page height
    // If both FitWidth and FitHeight are true then the HTML content will resized if necessary to fit both width and height 
    // preserving the aspect ratio at the same time
    // By default this property is false and the HTML content is not resized to fit the PDF page height
    htmlToPdfConverter.PdfDocumentOptions.FitHeight = fitHeightCheckBox.Checked;

    // Use this option to render the whole HTML content into a single PDF page
    // The PDF page size is limited to 14400 points
    // By default this property is false
    htmlToPdfConverter.PdfDocumentOptions.SinglePage = singlePageCheckBox.Checked;

    string url = urlTextBox.Text;

    // Convert the HTML page to a PDF document using the scaling options
    byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);

    // Send the PDF as response to browser

    // Set response content type
    Response.AddHeader("Content-Type", "application/pdf");

    // Instruct the browser to open the PDF file as an attachment or inline
    Response.AddHeader("Content-Disposition", String.Format("attachment; filename=HTML_Content_Scaling.pdf; size={0}", outPdfBuffer.Length.ToString()));

    // Write the PDF document buffer to HTTP response
    Response.BinaryWrite(outPdfBuffer);

    // End the HTTP response and stop the current page processing
    Response.End();
}
梦纸 2024-12-13 09:31:27

如果您将 PdfConverter.PageWidth 和 PdfConverter.PageHeight 属性设置为 842 和 595,那么这应该会导致您的 Web 内容填充整个 PDF 页面区域。

还有一个 FitWidth 属性,如果您希望 Web 内容不缩小以适应可用页面区域,则可以将其设置为 False。这意味着如果您的内容太大,将会溢出页面。

关于 Winnovative PDF 需要提出的另一点是,处理高分辨率图像并不容易,因此根据您的 PDF 需求(和输入),您可能很难获得高质量的图像 - 也许这只是我的经验。

说到图像,Winnovative PDF 似乎会获取您输入的 html 并创建所有图像的单个图像,然后以屏幕​​分辨率 (72 dpi) 添加到 PDF,这甚至会使简单的文本看起来质量较低。

如果您想尝试自己处理 html 布局,您始终可以使用其他产品(例如 )将内容直接转换为 PDF ITextSharp 可以为更高分辨率的图像提供更大的灵活性。不过我真的取决于你的总体需求

If you set the PdfConverter.PageWidth and PdfConverter.PageHeight attributes to 842 and 595 then that should cause your web content to fill the whole PDF page area.

There is also a FitWidth property which can be set to False if you want your web content to not be scaled down to fit the available page area. This will mean that your content will overflow the page if it is too large though.

Another point to raise about Winnovative PDF is that it is not easy to work with high resolution images, so depending on your PDF needs (and input) you may struggle to get good quality images - maybe that was just from my experience though.

Speaking of images, it seems that Winnovative PDF takes your input html and creates a single image of it all which then gets added to the PDF at screen resolution (72 dpi) and this can even make simple text look low quality.

If you fancied having a go at handle the html layout yourself you could always convert your content straight to PDF using another product such as ITextSharp which would allow more flexibility over higher resolution images. I really depends on your overall needs though

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文