How to take webpage screenshots with wkhtmltopdf? - screenshot

I can create PDF file with wkhtmltopdf in linux Ubuntu: "wkhtmltopdf www.stackoverflow.com file.pdf"
How to take webpage screenshots?

You can use wkhtmltoimage as suggested by Mathew. You can go to the first URL and download the file suitable for your architecture (or its source code). Usage is simple:
./wkhtmltoimage-i386 http://www.google.com google.png

wkhtmltoimage - this also uses the webkit render engine for excellent results and is available as standalone binary with no install problems anticipated, just trial and error to get the results you need.

Related

Changing default path for Chrome executable for karate-chrome

I am using the Karate framework to do the API testing. As part of CI efforts, we send an email at the end of test execution listing the summary of test results. There is a need to include the screeshot of the test execution counts from 'overview-feature.html' file.
I did so through the TestRunner.java file - launched Chrome using Chrome.start() and then using it to take screenshot. It all works well locally on Windows.
However when executing on CI server which is a Unix box, the chrome executable is not present in the default location (usr/bin/google-chrome) and hence the connection for the localhost fails.
Is there a way we can change the default location of the chrome executable?
PS: Apologies if this was too trivial to be asked.
Yes Chrome on CI is hard to get right, refer: https://stackoverflow.com/a/62325328/143475 - note that CI boxes typically are "headless" a browser may not be even installed.
I think the best thing for you is to ZIP the HTML and send it. But I really think you need to work with some CI experts, because the report generation and e-mailing business is normally done by things like Jenkins. What you are doing is certainly not normal or best-practice.
If you really want, there is a Karate Docker container that can give you a proper Chrome instance (see docs) but that is overkill for what you need.
EDIT: The Chrome Java API allows for customization of the executable path and this is in the docs: https://github.com/intuit/karate/tree/master/karate-core#chrome-java-api
It should be something like this:
Chrome.start("/opt/blah/chrome");

Running tesseract 4.1 with openjpeg2 - cannot produce pdf output

I have installed on my RedHat machine:
(py36_maw) [rvp#lib-archcoll box]$ tesseract -v
tesseract 4.1.0
leptonica-1.78.0
libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7 : libopenjp2 2.3.1
Found SSE
I try to run, per what docs I can find, to produce pdf output:
(py36_maw) [rvp#lib-archcoll box]$ time tesseract test.jp2 out -l eng PDF
read_params_file: Can't open PDF
Tesseract Open Source OCR Engine v4.1.0 with Leptonica
Warning: Invalid resolution 0 dpi. Using 70 instead.
Estimating resolution as 275
That takes 10 seconds and produces file out.txt with fine OCR to text conversion evident.
However, it tries to read a file called PDF, but I cannot figure how to get PDF output.
I have read various docs, the most promising seeming to be advising to edit the config file, but the only docs I can guess are relevant, by googling 'tesseract 4.1 config', list many 'config' variable names, for older versions of tesseract, but none of which seems to indicate I can specify producing pdf output, much less specifically for tesseract 4.1.
How can I invoke tesseract 4.1 (using libopenjp2 2.3.1) via CLI to produce pdf output from my jp2 input file? Bonus question: how can I get it to produce both txt and pdf output in one run?
Robert
After more surfing and digging, assuming the reader also has done some and knows what TESSDATA_PREFIX is used for by tesseract, here are the steps that worked for me:
Download the pdf.ttf file from: https://github.com/tesseract-ocr/tesseract/blob/master/tessdata/pdf.ttf
Copy pdf.ttf to your directory $TESSDATA_PREFIX and make sure that variable is exported to your shell.
TIP: Use command: tesseract --print-parameters # to discover defined variable names you can use in your own config file
Go to your dir with the test.jp2 file and create file config with these lines.
tessedit_create_pdf 1 Write .pdf output file
tessedit_create txt 1 Write .txt output file
(Note: or you may be able to put the config file in the TESSDATA_PREFIX directory as well and let it always be the default. Not tested.)
Run in that dir:
$ tesseract test.jp2 outputbase -l eng config
Verify your success: it runs and produces files outputbase.txt and outputbase.pdf. The txt file looks good and the searchable pdf looks and works OK in a pdf viewer, that is, you can search and find text strings.
Hope this helps someone else!

error Converting PDF to PNG - Python 3.6 and GhostScript

I have much trouble to have a code to convert pdf file to png on python 3.6, windows 10.
I know what you are going to say : google it !
But barely everything I've found was on python 2.7. And some packages haven't been updated.
What I've seen so far it's that the best way to do it is using Wand, right ? (I have installed ImageMagick before )
from wand.image import Image
# Converting first page into JPG
with Image(filename='0.pdf') as img:
img.save(filename="/temp.jpg")
# Resizing this image
Here was my second error :
wand.exceptions.DelegateError: PDFDelegateFailed
`The system cannot find the file specified.' # error/pdf.c/ReadPDFImage/809
So i read i need ghostscript. I installed it. But the package is for python 2.7 and it doesn't work. I found python3-ghostscript 0.5.0. https://pypi.python.org/pypi/python3-ghostscript/0.5.0
New error :
RuntimeError: Can not find Ghostscript DLL in registry
So here I needed to install Ghostscript 9 :
https://www.ghostscript.com/download/gsdnld.html
First of all it's not a GPL license ... That's not even a package but a program. I don't know how I can use it in my futures python codes...
and there is still an error :
RuntimeError: Can not find Ghostscript DLL in registry
and i can't find anything for it.
Ghostscript is licensed under the AGPL, the licence can be found in /Program Files (x86)/gs/gs9.21/doc if you want sources then they are available from the Ghostscript Git repository. Note I'm assuming you are running on Windows since you refer to the Registry.
If you install the prebuilt binary then it will create an entry in the Windows Registry, I assume that's what your Python code is looking for but I can't be sure. You should make sure you install the correct word size (32 or 64) version required by Python, if it cares.
You can, of course, simply run Ghostscript to render a PDF file and produce PNG output.
gswin32c -sDEVICE=png16m -sOutputFile=out%d.png input.pdf
This will create one file per page of the input PDF file, use gswin64c for the 64-bit version...
You can alter the resolution of the output with the -r switch, eg -r300
I presume you can simply fork a process from Python. Otherwise you'll have to get someone to tell you what the Python script is looking for in the Registry. Perhaps its looking for a specific version of Ghostscript, or the 32-bit version or something.

Selenium/Watir can't detect Chromedriver, even though it's in my path (Windows 7)

I know there's another question similar to this, but the link provided by the answerer is out of date (and when I dig into the current Watir documentation I only find info on Windows 8.1; I am on Windows 7, and the step that doc suggests for resolving this is not helping me per below)
I am attempting to execute a test using Watir/minitest (along the lines of rake ci:setup:minitest test TEST=test/full_path/happypath_test.rb), and it chokes on the following:
Selenium::WebDriver::Error::WebDriverError: Unable to find the chromedriver exec
utable. Please download the server from http://chromedriver.storage.googleapis.c
om/index.html and place it somewhere on your PATH. More info at http://code.goog
le.com/p/selenium/wiki/ChromeDriver.
The Watir documentation and error message both point to the PATH. However, I have just downloaded the latest version of Chromedriver (2.12, it appears) and plonked the exe in a folder that I have confirmed is in my PATH (specifically c:/Ruby193/bin - yes I know I'm on an old Ruby version; alas not something I can update for this project)
I know Chromedriver is present because I can run it from irb:
require 'watir-webdriver
followed by
b = Watir::Browser.new :chrome
This launches a chrome window just fine. But when I actually try to execute a test it gives the "can't find chromedriver" error.
I've run bundle install to try and be sure I'm on the right versions of things and don't know what else to do. Any help, please?
Specify the driver_path directly to Selenium.
Simply call this before launching your new browser window:
Selenium::WebDriver::Chrome::Service.driver_path = "/absolute/path/to/chromedriver"
This also has the benefit of not requiring you to muck up your PATH and you can checkthis into your repo (of course, make the path dynamic to your project root directory, etc.).
I found real nice posts here and here. You can configure in super easy steps.
I have tried and tested working superb.
From post 1:
Change from firefox to chrome (wherever you have instantiated the browser ), so that it will become:
#browser = Watir::Browser.new :chrome
Now if you are on Windows, copy paste the downloaded binary file in C:/Windows directory`
Second post tells about how to change the default path c:\windows to your project's custom path. Its interesting and helpful. Take a look for sure.

wkhtmltopdf - cannot convert local file

I just downloaded version 10.0 for opensuse v. 11.3. I can convert a webpage (ie www.google.com) using it but cannot convert a local file. I grant all permissions on the file (& i've even tried running under sudo to no avail). This is the error:
"Loading pages (1/6) Error: Failed loading page file:///file.html".
The file exists but wkhtmltopdf refuses to load it. I even tried version 9.9 w/ the same result
What am I missing?
Just as a notice to any future people. I couldn't get the accepted answer working on Ubuntu 14.04, so I resorted to piping my HTML to the wkhtmltopdf command like so:
cat test.html | wkhtmltopdf - test.pdf
This is from the wkhtmltopdf command-line help:
Use - for stdin or stdout
You need to provide the full path. Using file:/// does not make use of the current working directory. Rather, it specifies the path to the file from the root of the file system.
Try with this option :
wkhtmltopdf --enable-local-file-access
It works for me.