Export as PDF using Selenium Webdriver Screenshot - pdf

Does anyone know if it's possible to export HTML to PDF using the screenshot feature in Selenium Firefox WebDriver? I have a webpage which has print specific css which I need to download automatically. I understand that the screenshot feature takes a screenshot of the page as an image, but I was looking for a scalable PDF file which is good for print.

Screenshots in Selenium are saved as PNG. And PNG and PDF are different kind of formats. So Selenium cannot save your HTML page image directly as a PDF.
But, you could try to insert the PNG screenshot that Selenium takes and add it to a PDF.
Check this answer. Basically, you will need a library (like itext) and do something like:
// Take screenshot
driver.get("http://www.yourwebpage.com");
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File("screenshot.png"));
// Create the PDF
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
PdfWriter.getInstance(document, new FileOutputStream("my_web.pdf"));
document.open();
Image image = Image.getInstance(getClass().getResource("screenshot.png"));
document.add(image);
document.close();
Hope it helps!
EDIT
Since webs can be pretty high, you will probably need to check the documentation to see how you want to set your image in a PDF file.

A quick and easy way is to build an HTML file and embed the images as base64 data. You can then use any converter to get the document as a PDF.
An example with Python:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://www.google.co.uk");
# open new file
file = open(r"C:\temp\captures.html", "w")
file.write("<!DOCTYPE html><html><head></head><body width=\"600px\">")
# write image
file.write("<img src=\"data:image/png;base64,")
file.write(driver.get_screenshot_as_base64())
file.write("\">")
# close file
file.write("</body></html>")
file.close()
driver.quit()

Webdriver doesn't support "Export As PDF" function.
When you are not bound to Firefox and Webdriver, phantomjs could be an alternative.
Phantomjs is a headless browser with the ability to take screenshots as PDF.
The browser can be controlled directly by javascript.
Example: http://phantomjs.org/screen-capture.html

Related

Selenium Python Upload a picture to Facebook

please help, I want to upload a picture, I tried a lot, it is not useful
bugh = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div[5]/div[3]/form/div[6]/div/button[1]/div/div[1]/i")))#.click()
bugh.send_keys(r'N:\g\kkkp.png')

Android camera, take picture(s) and save as multipage PDF, then upload to server via <input type="file" />

I have a webform with and want to open it on smartphone - than take pictures of some documents which need to be merged in one PDF, and on the end this file need to be uploaded to server.
My solution is to use Google Drive to upload PDF (scan) to GDrive and then somehow download this file from gdrive to server via some sort of widget (any links appreciate) installed on website.
Maybe someone have a better idea?
I know its late but my answer might help others. I also face the same challenge and implemented a custom solution based on Javascript and Since you are using web form so this solution will perfectly fits on your need.
You have to use JSPdf javascript library, JSPdf provide you pdf object in your browser and you can upload it download it and there are many other thing to play with.
First you have to initialize JSPdf object as per your requirement. I am creating PDF with page size width:500px and height 500px.
pdf = new jsPDF("l", "pt", [500,500]);
Simply when you will take picture from camera you will have each picture in form of base64, that base64 format you have to insert in JSPdf object
pdf.addImage(imgData, 'JPEG', 0, 0);
you can repeat the above code to add pictures from camera as much as you want, at the back-end these images are compiling and creating pdf document where each page have each images in sequence.
Once you are done, you can get PDF object in form of base64 object using below code that you can upload to any server.
pdf.output('datauristring')
above is only pdf part, you can find complete working example including camera part here Javascript Component to Scan Document

Show PDF in a Vaadin View

I want to show a PDF in a Vaadin view. I create a BrowserFrame and I pass the StreamResource of the PDF:
StreamResource streamResource = new StreamResource(
getPresenter().createPdfStreamSource(), report.getName() + ".pdf");
BrowserFrame pdf = new BrowserFrame(null, streamResource);
pdf.setSizeFull();
content.addComponent(pdf);
This code creates an iframe with the following source:
http://localhost:8080/my-app/vaadinServlet/APP/connector/0/134/source/mypdf.pdf
If I point the browser to this URL, it shows the PDF properly. However in my application the PDF view doesn't show. What am I doing wrong?
I am testing this in Google Chrome.
Edit: I downloaded the plugin PDF Viewer based on PDF.js and my app works fine. Does the Chrome PDF reader plugin need special configuration?
An iframe is probably not the best option to render a PDF, especially on mobile browsers. Check the WT PDF Viewer add-on which is based on pdf.js for a more compatible solution.

Display pdf in iframe using ssl/https based

I want to Showing PDF inside iframe for user preview before download the file or print,
i use byte array convert my report file to pdf then showing it.
everything was perfect until i need something in ssl/https.
because of that i must change my application to ssl/https can some one show me how can i show pdf in this condition.
thanks for listening and reading my prob.
here my code
reportDocument.Load(reportPath);
reportDocument.SetDataSource(dataSet);
_contentBytes = StreamToBytes(reportDocument.ExportToStream(ExportFormatType.PortableDocFormat));
.....
//setting header
.....
//then flush
stream.Flush();
i found the solution in
PDF conversion suddenly fails if reading stylesheet from SSL
the problem is the pdf reader inside my chrome browser.
so i updating my chrome reader in store search pdf viewer..
just it..

Export SVG elements to PDF?

I have a visualization generated by d3 (a javascript visualization library similar to Protovis or Raphael, which draws stuff using SVG elements). The vis is interactive, so the user can interact with and edit it. Once the user is satisfied with his/her visualization, I would like the user to be able to export this visualization as a PDF. I've tried several HTML to PDF libraries and they don't work with SVG elements.
It is okay if the solution is either client side or server side. I'm using PHP server side but Python or Java implementations might also work.
Browser support: Ideally it would support all modern browsers, but minimally I'd like to support latest versions of both Firefox and webkit browsers.
I do not know of any strong PDF libraries on the client side.
A quick possible way would be to send the svg content to a server, and use something like batik for java to turn the svg to pdf and then send the response to the client again.
Here is a related SO for the converstion.
There's also wkhtml2pdf, which can render anything webkit can as a PDF. If you want to render a combination of SVG and HTML, or want to have some JavaScript run before the PDF snapshot is taken, it's great for that.
http://code.google.com/p/wkhtmltopdf/
PhantomJS can also rasterize url/html to PDF. Same backend (QTWebKit) with wkhtml2pdf.
I did not try d3, but I achieved the effect you are looking for like this in Python3.6:
# Pdf library
from reportlab.pdfgen import canvas
from reportlab.graphics import renderPDF, renderPM
# Svg library
import svgwrite
# Svg to reportlab
from svglib.svglib import svg2rlg, SvgRenderer
# Xml parser
from lxml import etree
# Create the svg
dwg = svgwrite.Drawing('test.svg', profile='tiny')
dwg.add(dwg.line((0, 0), (10, 10), stroke=svgwrite.rgb(10, 10, 16, '%')))
dwg.add(dwg.text('Test', insert=(0, 0.2)))
# Create canvas for pdf
c = canvas.Canvas("output.pdf")
# Parse the xml of the svg
parser = etree.XMLParser(remove_comments=True, recover=True)
root = etree.fromstring(dwg.tostring())
# Render the svg itself
svgRenderer = SvgRenderer()
drawing = svgRenderer.render(root)
# Now render the drawing in the pdf
renderPDF.draw(drawing , c, 10, 10)
# End page and save pdf file
c.showPage()
c.save()
# Or render to a seperate png
renderPM.drawToFile(drawing, "file.png", fmt="PNG")
Reportlab is an open source pdf library and svglib is a library that is able to convert svg's to reportlab Drawings. Rendering svg's directly from the xml is not supported out of the box, that is why I use the SvgRenderer.