使用 ASP.NET 生成打印质量 PDF

发布于 2024-11-17 12:34:58 字数 243 浏览 4 评论 0原文

我最近在网站上创建了一个功能,该功能将生成包含图像和文本的 PDF 文档。然后这些将被打印出来并粘贴到咖啡杯上。

我使用 Winnovative HTML 到 PDF 转换来完成此操作。

一切正常,但图像质量太低,无法打印,而且在杯子上看起来很垃圾。

有人告诉我这样做的原因是,当图像需要 300dpi(打印质量)时,图像只能达到 70dpi(屏幕质量)。

有没有办法生成这些 PDF 以使图像具有打印质量?

I've recently created a feature on a website which will generate a PDF document containing images and text. These will then be printed out and stuck onto coffee mugs.

I've done this using Winnovative HTML to PDF conversion.

Everything works fine, however the image quality is far too low for print, and looks rubbish on the mugs.

I've been told the reason for this is because the image will only be 70dpi (screen quality), when it needs to be 300dpi (print quality).

Is there a way I can generate these PDFs so that the images are print quality?

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

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

发布评论

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

评论(2

余生共白头 2024-11-24 12:34:58

我对 iTextSharp 的使用真的很幸运,我用它构建了许多高质量的 pdf 文件。但我从头开始构建 pdf,然后直接打印。您可以在代码中重新创建页面内容并打印吗?做起来非常简单,并且有很多资源/示例可供构建。

如果没有,有几种产品,例如 pdfcrowd。我不能说我自己用过它。但我听说有人用它来获得高质量的 html 到 pdf,但你必须为此特权付费。

I have really good luck with iTextSharp, I build many good quality pdfs with it. But I build the pdfs from scratch, and print directly. Would it be possible for you to recreate the page content in the code and print? It's pretty simple to do, and there is a lot of resources/examples out there to build from.

If not, there's several products out there like pdfcrowd. I can't say I've used it myself. But I've heard of people using it for high quality html to pdf, but you have to pay for the privilege.

江南烟雨〆相思醉 2024-11-24 12:34:58

当 PDF 中有光栅图像时,其质量会变得越来越低,并且您会放大 PDF 文档。为了更好地进行比较,您应该在 PDF 查看器中设置 100% 的缩放级别。如果您想要在 PDF 中使用更高分辨率的图片而不是 HTML 中的图片,您可以执行此操作,如 在 PDF 演示中用更高质量的图像替换 HTML 中的图像 。该演示中的相关 C# 代码是:

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=";

    // Select all images from HTML page
    htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { "img" };

    // Exclude the original images from rendering becuase they will be replaced by an image from local file system
    htmlToPdfConverter.HiddenHtmlElementsSelectors = new string[] { "img" };

    Document pdfDocument = null;
    try
    {
        // Convert a HTML string with images to replace to a PDF document object
        pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text);

        // Replace the images selected in HTML using special attributes with images from local file system
        foreach (HtmlElementMapping imageElementInfo in htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult)
        {
            PdfPage imagePdfPage = imageElementInfo.PdfRectangles[0].PdfPage;
            RectangleF imageRectangle = imageElementInfo.PdfRectangles[0].Rectangle;

            ImageElement newImageElement = new ImageElement(imageRectangle.X, imageRectangle.Y, imageRectangle.Width, imageRectangle.Height,
                            Server.MapPath("~/DemoAppFiles/Input/Images/box.jpg"));
            newImageElement.EnlargeEnabled = true;
            imagePdfPage.AddElement(newImageElement);
        }

        // Save the PDF document in a memory buffer
        byte[] outPdfBuffer = pdfDocument.Save();

        // 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=Replace_with_Higher_Quality_Images.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();
    }
    finally
    {
        // Close the PDF document
        if (pdfDocument != null)
            pdfDocument.Close();
    }
}

When you have a raster image in PDF its quality becomes lower and lower and you zoom in PDF document. For a good comparison you should have 100% zoom level in PDF viewer. If you have a higher resolution picture you want to use in PDF instead of the picture from HTML the you can do this as you can see in the Replace Images from HTML with Higher Quality Images in PDF Demo . The relevant C# code from that demo is:

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=";

    // Select all images from HTML page
    htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementSelectors = new string[] { "img" };

    // Exclude the original images from rendering becuase they will be replaced by an image from local file system
    htmlToPdfConverter.HiddenHtmlElementsSelectors = new string[] { "img" };

    Document pdfDocument = null;
    try
    {
        // Convert a HTML string with images to replace to a PDF document object
        pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text);

        // Replace the images selected in HTML using special attributes with images from local file system
        foreach (HtmlElementMapping imageElementInfo in htmlToPdfConverter.HtmlElementsMappingOptions.HtmlElementsMappingResult)
        {
            PdfPage imagePdfPage = imageElementInfo.PdfRectangles[0].PdfPage;
            RectangleF imageRectangle = imageElementInfo.PdfRectangles[0].Rectangle;

            ImageElement newImageElement = new ImageElement(imageRectangle.X, imageRectangle.Y, imageRectangle.Width, imageRectangle.Height,
                            Server.MapPath("~/DemoAppFiles/Input/Images/box.jpg"));
            newImageElement.EnlargeEnabled = true;
            imagePdfPage.AddElement(newImageElement);
        }

        // Save the PDF document in a memory buffer
        byte[] outPdfBuffer = pdfDocument.Save();

        // 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=Replace_with_Higher_Quality_Images.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();
    }
    finally
    {
        // Close the PDF document
        if (pdfDocument != null)
            pdfDocument.Close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文