Jump to content

Recommended Posts

Posted

Hi all,

With IE, I can use the following code to Open a html file invisible and use IE to call execWB to print that file without any GUI poping up as well. The whole process looks so smoothly and professionally in term of user interaction with this method. 

$oIE= _IECreate ("C:\Test\ShippingLabelV3.html",0,0)
$oIE2.execWB(6,2)

Would you have any suggestion that we can achieve the same goal with either Firefox or Chrome in WebDriver? Your feedback is much appreciated. 

Posted (edited)

Hi Danp2,

Thank you for your suggestion. I never thought about the _WD_PrintToPdf. So I tried this 

_WD_Navigate($oFF,"file:///C:/BER/PackingSlipV1.html")

    _WD_PrintToPdf($oFF,"C:\BER\TEst.pdf)

but it does not create any test.pdf file. 

I believe that I did something wrong with this line _WD_PrintToPdf($oFF,"C:\BER\Test.pdf"). Do you have any idea?

(The browser did open the html file successfully )

 

Edited by PnD
Posted

Hi Danp2,

Thank you for your guidance. I searched over the forum and found this thread that seems to be relevant to our topic

However, before I could even give this one a try, I encountered an issue with _WD_PrintToPdf where the log said:

_WD_IsLatestRelease: True
_WD_IsLatestRelease ==> Success
_WD_Startup: OS:    WIN_10 WIN32_NT 19041 
_WD_Startup: AutoIt:    3.3.14.5
_WD_Startup: WD.au3:    0.4.0.4 (Up to date)
_WD_Startup: WinHTTP:   1.6.4.2
_WD_Startup: Driver:    msedgedriver.exe
_WD_Startup: Params:    --verbose --log-path="C:\Autoit Webdriver\msedge.log"
_WD_Startup: Port:  9515
__WD_Post: URL=HTTP://127.0.0.1:9515/session; $sData={"capabilities": {"alwaysMatch": {"ms:edgeOptions": {"binary": "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe", "excludeSwitches": [ "enable-automation"],"args":["--user-data-dir=C:\\Users\\phong.nguyenduc\\AppData\\Local\\Microsoft\\Edge\\User Data\\", "--profile-directory=Default"], "useAutomationExtension": false}}}}
__WD_Post: StatusCode=200; ResponseText={"value":{"capabilities":{"acceptInsecureCerts":false,"browserName":"msedge","browserVersion":"91.0....
_WD_CreateSession: {"value":{"capabilities":{"acceptInsecureCerts":false,"browserName":"msedge","browserVersion":"91.0.864.54","ms:edgeOptions":{"debuggerAddress":"localhost:58111"},"msedge":{"msedgedriverVersion":"91.0.864.54 (c789bf186b966721dc5a5b881c7f11b1934f0f0f)","userDataDir":"C:\\Users\\phong.nguyenduc\\AppData\\Local\\Microsoft\\Edge\\User Data\\"},"networkConnectionEnabled":false,"pageLoadStrategy":"normal","platformName":"windows","proxy":{},"setWindowRect":true,"strictFileInteractability":false,"timeouts":{"implicit":0,"pageLoad":300000,"script":30000},"unhandledPromptBehavior":"dismiss and notify","webauthn:extension:largeBlob":true,"webauthn:virtualAuthenticators":true},"sessionId":"c34e13fbd5b775fb23f6b5477c68f8b2"}}
__WD_Post: URL=HTTP://127.0.0.1:9515/session/c34e13fbd5b775fb23f6b5477c68f8b2/url; $sData={"url":"http://www.google.com"}
__WD_Post: StatusCode=200; ResponseText={"value":null}...
_WD_Navigate: {"value":null}
__WD_Post: URL=HTTP://127.0.0.1:9515/session/c34e13fbd5b775fb23f6b5477c68f8b2/print; $sData={}
__WD_Post: StatusCode=500; ResponseText={"value":{"error":"unknown error","message":"unknown error: PrintToPDF is only supported in headless...
__WD_Post ==> Webdriver Exception: {"value":{"error":"unknown error","message":"unknown error: PrintToPDF is only supported in headless mode\n  (Session info: MicrosoftEdge=91.0.864.54)","stacktrace":"Backtrace:\n\tOrdinal0 [0x00007FF6ECC5A072+4431986]\n\tOrdinal0 [0x00007FF6EC90D883+972931]\n\tOrdinal0 [0x00007FF6EC9161DD+1008093]\n\tOrdinal0 [0x00007FF6EC989713+1480467]\n\tOrdinal0 [0x00007FF6EC963056+1323094]\n\tOrdinal0 [0x00007FF6EC975131+1397041]\n\tOrdinal0 [0x00007FF6EC962ECF+1322703]\n\tOrdinal0 [0x00007FF6EC9389FA+1149434]\n\tOrdinal0 [0x00007FF6EC939A5F+1153631]\n\tOrdinal0 [0x00007FF6EC9CBEFD+1752829]\n\tOrdinal0 [0x00007FF6EC9CA492+1746066]\n\tOrdinal0 [0x00007FF6ECB48F24+3313444]\n\tOrdinal0 [0x00007FF6ECC79D09+4562185]\n\tOrdinal0 [0x00007FF6ECC1112E+4133166]\n\tOrdinal0 [0x00007FF6ECC10684+4130436]\n\tOrdinal0 [0x00007FF6ECC104D5+4130005]\n\tOrdinal0 [0x00007FF6ECC2A8CF+4237519]\n\tBaseThreadInitThunk [0x00007FFFEB357034+20]\n\tRtlUserThreadStart [0x00007FFFEB8A2651+33]\n"}}
_WD_Window: {"value":{"error":"unknown error","message":"unknown error: PrintToPDF is only supported in headless...
_WD_Window ==> Webdriver Exception: HTTP status = 500
_WD_PrintToPDF ==> Webdriver Exception
+>13:16:54 AutoIt3.exe ended.rc:0
+>13:16:54 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 8.517

and this is just my simple code for testing, and both of them are generating the same error logs above.

_WD_Navigate($oFF,"http://www.google.com")
   sleep(1000)
   $a=_WD_PrintToPdf($oFF)
    ConsoleWrite($a)
    
    or 
    
   _WD_Navigate($oFF,"file:///C:/BER/PackingSlipV1.html")

   $a= _WD_PrintToPdf($oFF)
    ConsoleWrite($a)

I know this may be too much to ask for, but would you help share with me your experience on how to parse it directly to a printer assuming that I can be able to get the string successfully from the function _WD_PrintToPdf?

 

Posted
  On 6/22/2021 at 8:31 PM, PnD said:

PrintToPDF is only supported in headless mode

Expand  

This is a limitation of the Chromium-based browsers. Hopefully the will resolve it at some point. Check out the function header for _WD_PrintToPDF for a link to the open issue.

  On 6/22/2021 at 8:31 PM, PnD said:

 I searched over the forum and found this thread that seems to be relevant to our topic

Expand  

I don't believe that this is needed. You should be able to write the string to a file using standard Autoit functions.

  On 6/22/2021 at 8:31 PM, PnD said:

how to parse it directly to a printer

Expand  

Unfortunately, this isn't something that I've needed to do yet. Simplest solution would be to output it to a file and then print the file with ShellExecute.

Posted

Thanks Dan! At this point, I may just have to stick around with IE for quite sometime when printing these stuffs until they came up the the non-headless mode solution. Thank you a lot for your guidance though. 

Posted

Hi Dan

I did the test with Firefox and _WD_PrintToPdf actually did produce the string. However, the file just lost its color format in which it turns to Black and White  only. The rest of the format such as Front Size and Frame looks identical to the original file. 

_WD_Navigate($oFF,"file:///C:/BER/PackingSlipV1.html")

   $a= _WD_PrintToPdf($oFF)
   MsgBox(0,0,$a)
   FileDelete("C:\BER\test.pdf")
   FileWrite("C:\BER\test.pdf",$a)
    ShellExecute("C:\BER\test.pdf","","","Print",@SW_HIDE)

Is this something abnormal?

image.png.d9b793c47580b067e00fb6fa132d497b.png

 

Posted

Hi Dan,

ShellExecute would work ok with the original html file except that I have to Send ({Enter}) to the pop-up Print Windows to print which defeats the purpose of doing it silently. 

Since I am printing black and white anyway, the ShellExcecute will print pdf without any Print Windows perfectly. 

It just that when Filewrite a string generate from _WD_PrintToPdf to a pdf file, somehow the colors were just removed as showed in the picture above as well. 

Posted

Did you try to print HTML to pdf manually via browser ?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 6/23/2021 at 6:01 AM, mLipok said:

Did you try to print HTML to pdf manually via browser ?

Expand  

Hi mLipok,

Yes, I called the file directly via 

_WD_Navigate($oFF,"file:///C:/BER/PackingSlipV1.html")

 

maybe it's the reason why the color lost?

Posted (edited)

No... I mean do not use WD .

Open this file manually in browser and print to pdf manually. 

Check what is going on with colors.

 

In such case Are they properly printed to pdf ?

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 6/23/2021 at 8:08 PM, mLipok said:

No... I mean do not use WD .

Open this file manually in browser and print to pdf manually. 

Check what is going on with colors.

 

In such case Are they properly printed to pdf ?

 

Expand  

I see what you mean. Interestingly, I try both Chrome and Firefox per your suggestion, and the color is showing again as long as the "Print backgrounds" option is checked on both browsers.  

 

With Print Background checked

image.png.05390ae5e796e79b142e8725836c5326.png

 

Print backgrounds is unchecked

image.png.5c1b21912fd6e8bcd7364a458736b17a.png

Posted (edited)

Exactly...

Now @Danp2 turn ;)

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

@PnD try:

_WD_Navigate($oFF,"file:///C:/BER/PackingSlipV1.html")
$a= _WD_PrintToPdf($oFF,'{"isCssBackgroundEnabled": True}')

or:

_WD_Navigate($oFF,"file:///C:/BER/PackingSlipV1.html")
$a= _WD_PrintToPdf($oFF,'{"background": True}')

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...