baskren / Forms9Patch

Simplify image management and text formatting in your Xamarin.Forms apps
http://Forms9Patch.com
Other
128 stars 33 forks source link

How to specify paper format for myHtmlString.PrintAsync #97

Open Kingdom-Of-Heaven opened 3 years ago

Kingdom-Of-Heaven commented 3 years ago

It works nice when i just use this code below. Nevertheless how can i set paper format like A4 or A5 ?

if (Forms9Patch.PrintService.CanPrint)
{
        var myHtmlString = @"
                            <!DOCTYPE html>
                            <html>
                            <body>

                            <h1>Convert to PNG</h1>
  </body>
                            </html>
                            ";

 await myHtmlString.PrintAsync("my_print_job_name");       
}
smalgin commented 3 years ago

Try this:

/// <summary>
    /// Converts HTML text to PNG
    /// </summary>
    /// <param name="html">HTML string to be converted to PDF</param>
    /// <param name="fileName">Name (not path), excluding suffix, of PDF file</param>
    /// <param name="pageSize">PDF page size, in points. (default based upon user's region)</param>
    /// <param name="margin">PDF page's margin, in points. (default is zero)</param>
    /// <returns></returns>
    public static async Task<ToFileResult2> ToPdfAsync(this string html, string fileName, PageSize pageSize = default, PageMargin2 margin = default)
    {

        _platformToPdfService = _platformToPdfService ?? DependencyService.Get<IToPdfService2>();
        if (_platformToPdfService == null)
            throw new NotSupportedException("Cannot get HtmlService: must not be supported on this platform.");
        ToFileResult2 result = null;
        using (var indicator = ActivityIndicatorPopup.Create())
        {
            if (pageSize is null || pageSize.Width <= 0 || pageSize.Height <= 0)
                pageSize = PageSize.Default;

            margin = margin ?? new PageMargin2();
            if (pageSize.Width - margin.HorizontalThickness < 1 || pageSize.Height - margin.VerticalThickness < 1)
                return new ToFileResult2(true, "Page printable area (page size - margins) has zero width or height.");

            result = await _platformToPdfService.ToPdfAsync(html, fileName, pageSize, margin);
        }
        await Task.Delay(50);
        return result;
    }
baskren commented 3 years ago

Thanks, @smalgin for helping!

smalgin commented 3 years ago

Thanks to you! AFAIR You can also set custom Margin in the latest version.

NOTE: proper pagination (eq footers, headers) is ANOTHER issue & requires significant time investment. It is on my TODO list, but with a low priority... either I will fix it in 2022 or by that time I'll have budget to pay somebody to do this for me :)

smalgin commented 3 years ago

@Robert969696 FYI https://github.com/baskren/Forms9Patch/blob/a743b3cfd43b685c6a98bc2cceca195fb6210794/Forms9Patch/Forms9Patch/Services/ToPdfService.cs

Kingdom-Of-Heaven commented 3 years ago

but why additionaly converting html to pdf?

smalgin commented 3 years ago

I use html as source format because I need some formatting and logo. Simple text only has paragraphs and indentation. Pick based on requirements

Kingdom-Of-Heaven commented 3 years ago

Hmm not working for me i did:

public async Task printme()
{
     if (Forms9Patch.PrintService.CanPrint)
     {

                var myHtmlString = @"
                            <!DOCTYPE html>
                            <html>
                            <body>

                            <h1>Something</h1>
                           </body>
                            </html>
                            ";

                 var aaa =    await myHtmlString.ToPdfAsync("MojaNazwaPliku", PageSize.IsoA5);

                 await aaa.Result.PrintAsync("my_print_job_name");   
     }
}

After that on pdf in printer preview i see text like: MojaNazwaPliku9827398798237489234.pdf innstead of my myHtmlString'

Kingdom-Of-Heaven commented 3 years ago

WHat am i doing wrong?

smalgin commented 3 years ago

I honestly don't know. I never did any printing - I just save PDF files. I suspect your result is a file name, that's why you see filename printed.

Kingdom-Of-Heaven commented 3 years ago

Yes i see that this line: await aaa.Result.PrintAsync("my_print_job_name"); gives me the created pdf's path.

Do you know how can i stream that pdf i mean how to convert it to string based on given path?