Remove bleed and slug while converting HTML to PDF


#1

I am trying to convert a HTML document back to PDF and remove margins from the document. My original document has bleed and slug added to it. I need to retain these margins in HTML and remove them when converting back to PDF. I am using the following code, but the background image in the result is stretched and is not positioned correctly. Also not sure if I am calculating the margin correctly.

    public static void ConvertToPdf(string sourcePdfPath, string sourceHtmlPath, string targetPdfPath)
    {
        HtmlLoadOptions htmlOptions = new HtmlLoadOptions(sourceHtmlPath);
        htmlOptions.ExternalResourcesCredentials = System.Net.CredentialCache.DefaultCredentials;

        using (Document originalDocument = new Document(sourcePdfPath))
        {
            Page modelPage = originalDocument.Pages.First();
            htmlOptions.PageInfo.Margin.Left = modelPage.MediaBox.LLX - modelPage.TrimBox.LLX;
            htmlOptions.PageInfo.Margin.Right = modelPage.TrimBox.URX - modelPage.MediaBox.URX;
            htmlOptions.PageInfo.Margin.Top = modelPage.TrimBox.URY - modelPage.MediaBox.URY;
            htmlOptions.PageInfo.Margin.Bottom = modelPage.MediaBox.LLY - modelPage.TrimBox.LLY;

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(sourceHtmlPath))))
            {
                using (Document newDocument = new Document(stream, htmlOptions))
                {
                    newDocument.Save(targetPdfPath);
                }
            }
        }
    }

Coud you please have a look and point me in the right direction?

Kind regards,

Stefaan


#2

@stefaan.vandewinkel

Thank you for contacting support.

Would you please share source and generated ZIP files while elaborating with screenshots that how do you expect the output to be, so that we may investigate further to help you out.


#3

EXPECTED_test export_slug.pdf (1.6 MB)
ORIGINAL_test export_slug.pdf (1.6 MB)
ISSUE_test export_slug_convert.pdf (4.5 MB)

Hi,

Thanks for the fast reply! Attached are my test files:

HTML_test export_slug.zip: The HTML files that I’m using to generate the PDF
ORIGINAL_test export_slug.pdf: The original PDF that was used to create the HTML files (with Aspose)
ISSUE_test export_slug_convert.pdf: The result when I try to convert the HTML files to PDF with the posted code
EXPECTED_test export_slug.pdf: The result that we are expecting, this was created by cropping the ORIGINAL_test export_slug.pdf document with Aspose.

Kind regards,

Stefaan


#4

Hi,

I could not upload the ZIP with HTML files, tried several times with multiple browsers, but it did not stick.

Here is the code I used to generate the HTML files from ‘ORIGINAL_test export_slug.pdf’:

        public static void ToHtml(string sourcePdfPath, string targetHtmlPath)
        {
            HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions();
            htmlSaveOptions.SplitIntoPages = false;

            using (Document pdfDoc = new Document(sourcePdfPath))
            {
                pdfDoc.Save(targetHtmlPath, htmlSaveOptions);
            }
        }

Kind regards,

Stefaan


#5

@stefaan.vandewinkel

Thank you for sharing requested data.

We have been able to reproduce it with Aspose.PDF for .NET 19.6. Therefore, a ticket with ID PDFNET-46543 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.