DEV Community

Drazan-Jarak
Drazan-Jarak

Posted on

Converting HTML file to PDF using Aspose.Words API

Introduction:

In this article, you will be able to observe and test how to convert an HTML file with images to PDF.
Actually, I got a request from a customer and he wanted to use Aspose.Words library for this task.

Prerequisites:

Our Aspose.Words libraries work on any platform and include – Aspose.Words for .NET, Aspose.Words for Java, Aspose.Words for C++, Aspose.Words for Android via Java, Aspose.Words for SharePoint, Aspose.Words for Reporting Services and Aspose.Words for Jasper Reports. For this article, we will use Aspose.Words for .NET which can be downloaded here.

Simple conversion:

Usually, the conversion from HTML to PDF is simple like this:

   Aspose.Words.LoadOptions options = new Aspose.Words.LoadOptions();
   options.LoadFormat = Aspose.Words.LoadFormat.Html;
   Aspose.Words.Document document = new Aspose.Words.Document(inputFile, options);
   document.Save(outputFile, Aspose.Words.SaveFormat.Pdf);

Potential problem:

A potential problem here could be that the images from the source document don't fit the page width in the PDF output.

Solution:

   //loading the HTML file
   Words.LoadOptions options = new Words.LoadOptions();
           options.LoadFormat = LoadFormat.Html;
   Words.Document doc = new Words.Document("source.htm", options);
   //iterate through the sections in the doc Document
   foreach (Section section in doc.Sections)
   {
      //change document margins – the numbers can be adjusted for your needs
      Words.PageSetup ps = section.PageSetup;
      double margin = ps.TopMargin;
      ps.TopMargin = margin * 0.35;
      ps.BottomMargin = margin * 0.35;
      double targetWidth = ps.PageWidth - ps.LeftMargin - ps.RightMargin;

      //finding all the shapes in the document
      NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
      foreach (Words.Drawing.Shape shape in    shapes.OfType<Words.Drawing.Shape>())
      {
         if (shape.HasImage)
         {
            //checking if the image fits the page 
            double imgWith = ConvertUtil.PixelToPoint(shape.Width);
            if (targetWidth < imgWith)
            {
                shape.Width = targetWidth;
            }
         }

      }
   }
   doc.Save("output.pdf", SaveFormat.Pdf);

Conclusion: The Aspose.HTML and Aspose.PDF APIs could also be used for the conversion, but in this request, the customer wanted to use Aspose.Words. You can find more examples on our Documentation page.

Top comments (0)