MiServer output pages to PDF for print

MiServer is Dyalog's APL-based web development framework

MiServer output pages to PDF for print

Postby Gantois on Wed Aug 28, 2024 3:35 pm

Hi!
How to convert MiServer output pages to PDF for print, exactly as used in Dyalog documentation? (the same top bar menu).

Example:
https://docs.dyalog.com/latest/APL%20Wo ... 0Guide.pdf

I think it is an app insn´t it? If so, somebody knows its name, and how to use, please?

Thanks
Marco Gantois
Gantois
 
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: MiServer output pages to PDF for print

Postby Vince|Dyalog on Thu Aug 29, 2024 1:06 pm

Hi Marco,

In Dyalog, using the htmlrenderer, I can generate a pdf from a MiServer site.

Is this what you want to do?

'hr' ⎕wc 'htmlrenderer' ('URL' 'https://miserver.dyalog.com/')
hr.PrintToPDF 'd:\tmp\mypdf.pdf'
1

Regards,

Vince
Attachments
mypdf.pdf
(78.91 KiB) Downloaded 953 times
Vince|Dyalog
 
Posts: 440
Joined: Wed Oct 01, 2008 9:39 am

Re: MiServer output pages to PDF for print

Postby Gantois on Sat Aug 31, 2024 1:54 pm

Нi Vince,
At first Thanks for your help.
Explaining my case better (example):

:Class teste: MildPage

∇ Render req;html
:Access Public
html←Example
req.Return html


∇ r←Example
r←'<!DOCTYPE html>'
r,←'<html>'
r,←'<body>'
r,←'<h4>This is a HTML page example</h4>'
r,←'<p>I would like the user to be able to receive this page in pdf for print</p>'
r,←'<img src="\Img\3001.png" alt="logo" >'
r,←'</body>'
r,←'</html>'

:EndClass



I tried without success:

∇ Render req;html
:Access Public
html←Example
'hr' ⎕wc 'htmlrenderer' ('HTML' html)
hr.PrintToPDF'D:\Tmp\mypdf.pdf'
req.Return html


Could you help me please?
Regards,
Marco
Gantois
 
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: MiServer output pages to PDF for print

Postby Brian|Dyalog on Sun Sep 01, 2024 2:39 pm

Hi Marco!

I'm a little confused by what you're attempting. First, it looks like you're using MiServer version 2? I wouldn't attempt to call HTMLRenderer from within your MildPage - I think that overcomplicates things. I think the easiest way, assuming I have some understanding of what you're trying to accomplish, is to run your MiSite and then point an external HTMLRenderer at it. For example, the following is a really simple function to print any webpage using HTMLRenderer.
Code: Select all
     ∇ url PrintTo file;h
[1]    'h'⎕WC'HTMLRenderer'('URL'url)
[2]    1 ⎕NDELETE file ⍝ remove eny existing file of the same name
[3]    ⎕DL 2 ⍝ give page time to load
[4]    h.PrintToPDF file
     ∇
If your site is running on http://localhost:8088, then
      'http://localhost:8088' PrintTo '/tmp/marco.pdf'

will print the page to the file /tmp/marco.pdf. You need to insert a delay in order to allow the page to finish rendering before we try to print it. There are more complex ways to accomplish waiting for the page to load, but a simple ⎕DL should suffice for this problem.

I hope this helps!
/Brian
User avatar
Brian|Dyalog
 
Posts: 120
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: MiServer output pages to PDF for print

Postby Gantois on Tue Sep 03, 2024 2:35 pm

Hi Brian, How are you?

I believe this is the solution to what I'm trying to do:
print the page to the pdf file and than submit it to printer from within PDF.

Yes. I am using MiServer 2.

I will test giving you a feedback.
Thank you very much

Regards
Marco
Gantois
 
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: MiServer output pages to PDF for print

Postby Gantois on Sat Sep 14, 2024 12:32 pm

Hi Brian!

The url PrintTo file function works nice but I still have some problems to complement the solution. This is a brief to see if I am on the right track:
We are developing a website for schools. One feature of the teacher module is to print multiples choice tests created by themselves. When the test is selected in the list an apl function (MiServer Class) build and return the html result (a test/quiz for students). Everything is working so far ...

My question is: How to create the pdf file (from test.htm to test.pdf) and then call it for printing?


The functions for tests are in the namespace abc below.

Problems after tests:
When the content file is changed (abc.HtmlTest) but not the file name, the PrintTo function keeps the last file content even if the last file no longer exists.

TEST 1: OK
abc.Html2Pdf ‘marco’

TEST 2: OK
a) abc.Html2Pdf ‘marco1’
b) change the content of HtmlTest
c) abc.Html2Pdf ‘marco2’

TEST 3: failed → did not recognize the change (b)
a) abc.Html2Pdf ‘marco3’
b) change the content of HtmlTest
c) Html2Pdf ‘marco3’

Another question:
Is it possible the program call the url after PrinTo as if the user had clicked on the url link? (perhaps by httpCommand or CURL)?

:Namespace abc

∇ Html2Pdf uid;content;url;filehtml;filepdf
url←'http://localhost:8088/Tmp/',uid,'.html'
filehtml←'C:/MiServer/etestes/Tmp/',uid,'.html'
filepdf←'C:/MiServer/etestes/Tmp/',uid,'.pdf'
content←HtmlTest
content #.Files.PutText filehtml
url PrintTo filepdf


∇ r←HtmlTest
r←'<!DOCTYPE html>'
r,←'<body>'
r,←'<h4>This is a HTML page example</h4>'
r,←'<p>From html to pdf for printing</p>'
r,←'<img src="professor.png" alt="logo" >'
r,←'</body>'
r,←'</html>'


∇ url PrintTo file;h
'h'⎕WC'HTMLRenderer'('URL'url)
1 ⎕NDELETE file ⍝ remove eny existing file of the same name
⎕DL 2 ⍝ give page time to load
h.PrintToPDF file


:EndNamespace

Could you help me please?
Greetings!
Marco
Gantois
 
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: MiServer output pages to PDF for print

Postby Brian|Dyalog on Sat Sep 14, 2024 1:11 pm

Hi Marco!

I'm currently at Dyalog24 and won't have a chance to look at your question in detail for a few days. I apologize for the delay, but wanted to let you know why I'm not able to reply more quickly.

/Brian
User avatar
Brian|Dyalog
 
Posts: 120
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: MiServer output pages to PDF for print

Postby gil on Sun Sep 15, 2024 6:47 am

I'm not sure what you are trying to achieve, but it might be easier to help you by clarifying a couple of things.

Usually it is the client that does the printing. If I use my web browser and want a site saved as pdf or sent to a printer, the web browser offers this (usually both options via a print command in the menu or as a shortcut).

If you are writing an APL program and you want to achieve the same thing programmatically, you can use the HTMLRenderer. This is basically a web browser and offers a built in print command.

If I understand you right, your users access a website you develop and serve with MiServer, so printing is not something you need to add support for, it is a feature of the browser. You could add a button to your website that triggers the print client side, but it isn't strictly necessary (see https://developer.mozilla.org/en-US/docs/Web/API/Window/print).

Webpages can be cached on the way from the server to the client to speed up browsing and reduce traffic. There are several techniques to force new content to be fetched every time, e.g. use the HTTP header directive
Code: Select all
Cache-Control: no-cache
. This is something you would need to configure on the server side (in MiServer in your case).

Good luck!
gil
 
Posts: 73
Joined: Mon Feb 15, 2010 12:42 am

Re: MiServer output pages to PDF for print

Postby Gantois on Sun Sep 15, 2024 1:01 pm

Hi Brian! Thank you for the feedback.

Hi Gil!
Thanks for your clarifications. Like you said, my users access a website we develop and serve with MiServer. The reports to be printed depends on the user's choice from a list of options.
I understood that your last recommendation is appropriate for my case. I will see your link.
I will se the tip about cache too.

Thank you,
Greetings
Marco
Gantois
 
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Re: MiServer output pages to PDF for print

Postby Gantois on Fri Oct 11, 2024 3:25 pm

Hi Brian, Vince and Gil!

Feedback after tests:
All of your recommendations worked well.
The Brian/Vince's solution (examples above) in my case was more appropriate.

Note for cache solution:
To always read the last file created, I had to put at the end of the URL:
?ver=',(⍕⎕ts)~' '

The Gil's solution is very simple and works nice but the button image is also printed.

<button onclick="window.print()">Print this page</button>'

Thanks everybody for attention!

/Marco Gantois
Gantois
 
Posts: 91
Joined: Thu Apr 17, 2014 9:44 am

Next

Return to MiServer

Who is online

Users browsing this forum: No registered users and 1 guest