Remove bleed and slug while converting HTML to PDF

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

@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.

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

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

@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.

Thanks for investigating our issue and for the fast responses. We are working on an implementation that requires this feature. Is it possible to give us an indication on when this fix would be available?

@stefaan.vandewinkel

Please note that the ticket has been logged under free support model and will be investigated on first come first serve basis. Therefore, it may take some months to resolve. As soon as we have some definite updates or ETA regarding ticket resolution, we will let you know.

Moreover, we also offer Paid Support , where issues are used to be investigated with higher priority. Our customers, who have paid support subscription, report their issue there which are meant to be investigated urgently. In case your reported issue is a blocker, you may please consider subscribing for Paid Support. For further information, please visit Paid Support FAQs .