Android Question Printing - Allows passing custom PrintAttributes, created with JavaObject.

Mark Stuart

Active Member
Licensed User
Longtime User
In THIS POST, Erel referenced:
V1.11 - PrintPdf2 - Allows passing custom PrintAttributes, created with JavaObject.

Where can I find the PrinAttributes and an example/demo of defining and using the PrintAttributes?
Specifically, for PrintHTML as well. I generated the html and printed it. The page margins need to be set as it prints outside the page margins.

Regards,
Mark Stuart
 
Last edited:

LucasHeer

Active Member
Licensed User
Longtime User
Could you possibly add the padding/margin to the HTML itself? Are you trying to print in letter format?

Maybe
CSS:
@media print {
  @page {
    margin: 1in; /* Sets a 1 inch margin on all sides */
  }
}

Or
HTML:
<body style="padding:100px">
    <h1>Title</h1>
</body>

I design my reports in HTML and use server PHP/Laravel laravel-dompdf library to do HTML->PDF conversion, but I adjust the body padding or page margin to increase page margin.
 
Upvote 0

LucasHeer

Active Member
Licensed User
Longtime User
In THIS POST, Erel referenced:
V1.11 - PrintPdf2 - Allows passing custom PrintAttributes, created with JavaObject.

Where can I find the PrinAttributes and an example/demo of defining and using the PrintAttributes?
Specifically, for PrintHTML as well. I generated the html and printed it. The page margins need to be set as it prints outside the page margins.

Regards,
Mark Stuart

This should do it:
B4X:
    Dim printer As Printer
    printer.Initialize("")
    printer.PrintHtml("job", $"<body style="padding:100px;"><b>Hello world!!!</b><br/>
<h1>second line</h1>
<img style="max-width:100%;" src="https://anywheredealer-media-prod.s3.amazonaws.com/dealers/4/vehicles/2652/photos/694265f1603f7-pick_1765959004587_687432.jpg"/></body>"$)

Without Padding:
Screenshot_20251219-193037.jpg


With Padding:
Screenshot_20251219-193022.jpg
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Thank you Lucas. I'll try it tomorrow.
OK, just tried it at 100px;
A bit too much but it worked.
So set it to 25px and that looks good.

Next is to set some more PrintAttributes like page number and similar settings.
I'll also need to learn about pagination and when to stop printing to a page and when to issue a page header.
I haven't seen anything like that on the forum here, so will have to go elsewhere.
Thought since a Printing library to be released by this company, that they would by now have covered all this.

Regards,
Mark Stuart
 
Last edited:
Upvote 0

LucasHeer

Active Member
Licensed User
Longtime User
Thank you Lucas. I'll try it tomorrow.
OK, just tried it at 100px;
A bit too much but it worked.
So set it to 25px and that looks good.

Next is to set some more PrintAttributes like page number and similar settings.
I'll also need to learn about pagination and when to stop printing to a page and when to issue a page header.
I haven't seen anything like that on the forum here, so will have to go elsewhere.
Thought since a Printing library to be released by this company, that they would by now have covered all this.

Regards,
Mark Stuart

I thought you were going to wait until tomorrow 🤣 That happens to me all the time too! lol!

This is actually possible with pure HTML/CSS/JS. You can use the built in CSS Page positions and create per-page content, or keep up with your content height by page. Here is an example of using built-in page CSS Page properties to have custom page numbers:

B4X:
Dim printer As Printer
    printer.Initialize("")
    printer.PrintHtml("job", $"
    <html>
        <style>
            footer {
                display: none;
            }

            @page {
                margin-bottom: 2cm;
                @bottom-center {
                    content: "Page " counter(page) " of " counter(pages);
                    font-family: Arial, sans-serif;
                    font-size: 12px;
                }
            }
        </style>
       
        <body style="padding:25px;">
            <div>
                <h1>First Page!!!</h1><br/>
                <h1>second line</h1>
                <img style="max-width:100%;" src="https://anywheredealer-media-prod.s3.amazonaws.com/dealers/4/vehicles/2652/photos/694265f1603f7-pick_1765959004587_687432.jpg"/>
           
            </div>
            <div style="break-after: page;"></div>
            <h1>second Page!!!</h1><br/>
            <div style="break-after: page;"></div>
            <h1>second Page!!!</h1><br/>
   
        </body>
    </html>
    "$)

Screenshot_20251219-200729.jpg
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Yep, too early in the night to try stuff you can't wait till the A.M. :)

Wow, that's great, thank you. I don't know html too well. Used to use it many years ago, but I'm sure it's been updated.
Anyway, I'm at the learning stage of using B4X to print data. This is definitely at the learning stages.
Typically, I end up going straight to want I need to do and that is to build a report with data.
In this case a list of users - UserID and UserName. Pretty simple. One record on each row. Something simple to start with. so I asked ChatGPT to show me. In the midst of that now.

Will hopefully report back with something concrete.

Mark Stuart
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Lucas, tried your html code and it displays as it shows in your images, thank you.
Any chance you know how to get the Print Preview to prompt me when Save to PDF option is used?Using that option currently does not prompt me or displays where it was saved.
Looked in Documents and Downloads - nada.
 
Upvote 0

LucasHeer

Active Member
Licensed User
Longtime User
Lucas, tried your html code and it displays as it shows in your images, thank you.
Any chance you know how to get the Print Preview to prompt me when Save to PDF option is used?Using that option currently does not prompt me or displays where it was saved.
Looked in Documents and Downloads - nada.

Hey sir!

ChatGPT should definitely be able to provide a great example for printing UserID and UserName list, especially if you provide it with the existing code you have 👍

Also! You may need to drill down further into the save dialog:

1766211600591.png


You'll click the save button, that will prompt you to the final folder browser screen:

1766211654421.png
 
Upvote 0
Top