How to set pdf layout as Portrait and choose PaperSize as Letter

Hi team,
I am just trying a very simple HTML to PDF conversion. But I want to specify following two things :

  • PDF output should be Portrait not landscape
  • Choose the PaperSize as PaperSize.Letter

I tried using PdfSaveOptions, PageInfo etc but somehow didn’t get any options to save above.
Any help with be highly appreciated.

Sample code :
string documentPath = Path.Combine(path, “template.html”);
string savePath = “c:\data\drawing-options.pdf”;
using var doc = new HTMLDocument(documentPath);

var options = new Aspose.Html.Saving.PdfSaveOptions()
{
HorizontalResolution = 300,
VerticalResolution = 300,
JpegQuality = 100,
};

options.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(970, 390), new Aspose.Html.Drawing.Margin(0, 0, 0, 0));
Converter.ConvertHTML(doc, options, savePath);

Converter.ConvertHTML(doc, new PdfSaveOptions(), streamProvider);
var memory = streamProvider.Streams.First();
return memory.ToArray();

@svikrant786

Could you please share your sample HTML in .zip format with us? We will test the scenario in our environment and address it accordingly.

Thanks @asad.ali , attaching our sample support.zip for your ref.
The ask is pretty simple, just want to set the Portrait = true (or isLandscape = false) and PaperSize = PaperSize.Letter while saving the PDF. Right now, when it goes to printer, it’s turning into landscape. support.zip (31.2 KB)

** Using .Net libs.

@svikrant786

We tried to generate a PDF using below code snippet in our environment and noticed that content was cut off in the output PDF.

var outputStream = new MemoryStream();
var htmlStream = File.Open(dataDir + @"dummy-template.html", FileMode.Open);

using (var htmlConfiguration = new Aspose.Html.Configuration())
{
 htmlConfiguration.Security |= Html.Sandbox.AutomaticFeatures;
 htmlConfiguration.Security |= Html.Sandbox.Scripts;

 htmlStream.Position = 0;
 using (var document = new Html.HTMLDocument(htmlStream, dataDir, htmlConfiguration))
 {
  var pdfRenderingOptions = new Html.Rendering.Pdf.PdfRenderingOptions();
  pdfRenderingOptions.Css.MediaType = Html.Rendering.MediaType.Screen; // could be print?
  pdfRenderingOptions.JpegQuality = 40;
  // If the content on a page will not fit the width of the page naturally, scale the content to fit
  pdfRenderingOptions.PageSetup.PageLayoutOptions = Html.Rendering.PageLayoutOptions.ScaleToPageWidth;
  pdfRenderingOptions.PageSetup.AnyPage.Size.Width = Html.Drawing.Length.FromInches(8.5);
  pdfRenderingOptions.PageSetup.AnyPage.Size.Height = Html.Drawing.Length.FromInches(11);
  pdfRenderingOptions.PageSetup.AnyPage.Margin = new Html.Drawing.Margin(0, 0, 0, 0);
  using (var device = new Html.Rendering.Pdf.PdfDevice(pdfRenderingOptions, outputStream))
  {
   // Render HTML to PDF
   document.RenderTo(device);
  }
 }

 htmlStream.Position = 0;
 outputStream.Position = 0;
}

using (var fileStream = File.Create(dataDir + "ExampleOutput.pdf"))
{
 outputStream.Seek(0, SeekOrigin.Begin);
 outputStream.CopyTo(fileStream);
}

ExampleOutput.pdf (61.4 KB)

Please try to print the attached PDF and see if it still gets printed in landscape mode? Also, please share the sample output PDF generated at your end.

Hi @asad.ali your code snippet is generating a plain text file for me without css etc & your attached output is still landscape.
Attaching my pdf output which was generated using following code -
string documentPath = Path.Combine(path, “template.html”);
string savePath = “c:\data\drawing-options.pdf”;
using var doc = new HTMLDocument(documentPath);

var options = new Aspose.Html.Saving.PdfSaveOptions()
{
HorizontalResolution = 300,
VerticalResolution = 300,
JpegQuality = 100,
};

options.PageSetup.AnyPage = new Aspose.Html.Drawing.Page(new Aspose.Html.Drawing.Size(970, 390), new Aspose.Html.Drawing.Margin(0, 0, 0, 0));
Converter.ConvertHTML(doc, options, savePath);output.pdf (63.7 KB)

@svikrant786

Can you please share how you are checking if the page is landscape? We have checked the document properties for both our and your outputs and there was a difference in width of both PDFs.

Please specify the path to resources (dataDir in below code) to render the PDF with style and formatting:

hey @asad.ali … your code sample helped us to reach the desired output. We played with our html a bit after that. It was a total different use of classes in your approach and it wasn’t part of the aspose examples.
Thank you :slight_smile:

@svikrant786

It is nice to hear that you were able to sort your issue out. Please keep using the API and feel free to create a new topic in case you face any issues.

1 Like

@asad.ali sorry to bother you again … but I’m getting “object reference” exception while using - “document.RenderTo(device)” as sampled in your code.
It works on local machine for windows but fails when we try to deploy to our linux env for hosting on kubernetes.
Just wondering if you have encountered it before or have been reported earlier.

The detailed error is as follows :
“System.NullReferenceException: Object reference not set to an instance of an object.
at . ​ ( , Char )
at . ​ ( )
at .( , , )
at ​ …ctor( , IBrowsingContext , )
at …ctor(Node , , )
at …ctor(Element , )
at ​ .(Element , ​ , , , , )
at ​ .(Element , ​ , , )
at .(Document , RenderingOptions , IDevice , ​ , , )
at .​ ​ (IDevice , ​ )
at .()
at ​ . ​ ​ (Renderer , ​ [] , IDevice , CancellationToken )
at Aspose.Html.Rendering.HtmlRenderer.(IDevice , CancellationToken , Element[] , Document[] )
at . ​ (IDisposable , IDevice , CancellationToken , Document[] )
at Aspose.Html.HTMLDocument.RenderTo(IDevice device)”

image.png (17.4 KB)

@svikrant786

Please make sure that all Windows fonts are properly installed in the Linux system where this issue is occurring. You can try and install msttcorefonts package and see if it resolves the issue.

1 Like

@asad.ali thanks for your suggestion. It worked out fine after installing fonts & had to install apt-utils in order to make it work.

Getting another issue while I am sending it to Printer and wanna check if you have learnt about it earlier… on some printers, the printout is fine but on some other network printers, I am getting “PDF Error” on the device or just getting a black page out. There is no error back to the code.
I tried using PdfViewer, but it’s using System.Drawing.Printing class which is not compatible outside windows platform.
Using TCP connection class to send pdf to printer as follows -

Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.NoDelay = true;
IPAddress ipAddress = IPAddress.Parse(ip);
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, Convert.ToInt32(port));

clientSocket.Connect(ipEndPoint);
clientSocket.Send(data);
clientSocket.Close();

@svikrant786

Please try using Aspose.PDF.Drawing package instead of Aspose.PDF. Please remove existing Aspose.PDF package and install Aspose.PDF.Drawing package. This will resolve the issue related to System.Drawing.Common not being compatible outside Windows environment. In case you still notice any issues, please share complete environment details along with sample PDF so that we can further proceed to assist you accordingly.

@asad.ali then it seems like there is no PdfViewer exposed via Aspose.Pdf.Drawing. I was just trying to follow the examples explained here to Print() using PdfViewer- Working with PDF printing - Facades|Aspose.PDF for .NET

@svikrant786

Would you please share the above requested details. We will log an investigation ticket and share the ID with you.

Hi @asad.ali … attaching a sample (ExampleOutput1.zip) for your use.
Environment : Win 10, .Net 6.0 based API, Canon printer

Just to add again - pdf is looking good and it prints OK on some printers when I send as stream but there is a msg - “Pdf Error” on Canon printer and nothing prints.
I have been using TCP/IP socket connection to send the stream.
To resolve, I tried using PDFViewer from Aspose so that I don’t have to send as stream, but there is a class compatibility issue as described before. ExampleOutput1.zip (51.6 KB)

@svikrant786

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-54698

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

thanks @asad.ali … is there a way to know the updates on the ticket?

@svikrant786

The ticket status can be viewed at the bottom of this forum thread. However, we will keep you posted via this forum thread once some progress is made towards ticket resolution. The ticket will be investigated on a first come first serve basis and as soon as it is closed, we will notify you. Please spare us some time.

@svikrant786

Beginning with the library version 23.10 the Aspose.Pdf.Drawing library contains the PdfViewer class and all printing-related APIs as well.

In Windows environments it relies on the same System.Drawing.Printing APIs, and may be used the same way as the one provided in the Aspose.Pdf library.

And in non-Windows environments the System.Drawing.Printing APIs aren’t available, and you are required to provide your own print handlers (examples are given at PdfViewer.CustomPrint | Aspose.PDF for .NET API Reference ).

Please note that for now the printing quality on Windows with Aspose.Pdf.Drawing may be somewhat lower than with Aspose.Pdf (especially for textual parts), and we recommend to print in higher resolutions. We continue working on the printing subsystem overhaul that will address this and other issues.

The issues you have found earlier (filed as PDFNET-54698) have been fixed in Aspose.PDF for .NET 24.2.