Created
May 27, 2024 11:54
-
-
Save Largo/1e4524a15323e087fc236797a5b214f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'capybara' | |
require 'capybara/dsl' | |
require 'selenium-webdriver' | |
require 'open-uri' | |
require 'combine_pdf' | |
require 'net/http' | |
require 'uri' | |
Capybara.default_driver = :selenium_chrome_headless # remove headless if want to see chrome | |
Capybara.app_host = 'http://127.0.0.1:3000' | |
module MyCapybara | |
class Session | |
include Capybara::DSL | |
def build_cookie_string(cookie) | |
cookie_str = "#{cookie[:name]}=#{cookie[:value]}" | |
cookie.each do |key, value| | |
next if key == :name || key == :value | |
field_name = key.to_s.split('_').map(&:capitalize).join('-') | |
cookie_str += (case key | |
when :expires | |
"; #{field_name}=#{value&.to_time&.httpdate}" if value | |
when :http_only, :secure | |
"; #{field_name}" if value | |
else | |
"; #{field_name}=#{value}" | |
end).to_s | |
end | |
cookie_str | |
end | |
def download_pdf_with_session(download_url, file_path) | |
# Use Capybara's current session cookies to download the file | |
cookies = page.driver.browser.manage.all_cookies | |
uri = URI.parse(download_url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Get.new(uri.request_uri) | |
request["Cookie"] = cookies.map { |cookie| build_cookie_string(cookie) }.join(";") | |
headers = page.driver.browser.execute_script('return {...navigator.userAgent};') | |
request['User-Agent'] = headers | |
response = http.request(request) | |
#binding.irb | |
File.open(file_path, 'wb') do |file| | |
file.write(response.body) | |
end | |
end | |
def login_and_download_pdf(username, password, download_url, local_pdf_path) | |
visit '/en/users/sign_in' # Adjust the path to your login page | |
fill_in 'person_login_identity', with: username # Adjust the field ID | |
fill_in 'person_password', with: password # Adjust the field ID | |
click_button 'Sign in' # Adjust the button text or ID | |
visit "http://127.0.0.1:3000/de/groups/14/people/600351" | |
# Get the current page's HTML | |
pdf_url = "http://127.0.0.1:3000/de/people/600351/membership.pdf" | |
download_pdf_with_session(pdf_url, "downloaded_pdf.pdf") | |
# Load the downloaded PDF and the local PDF | |
pdf2 = CombinePDF.load("downloaded_pdf.pdf", allow_optional_content: true) | |
pdf1 = CombinePDF.load(local_pdf_path, allow_optional_content: true) | |
# Check if both PDFs have the same number of pages | |
if pdf1.pages.count == pdf2.pages.count | |
# Iterate over each page and overlay | |
pdf1.pages.each_with_index do |page, index| | |
page << pdf2.pages[index] | |
end | |
# Save the combined PDF | |
pdf1.save("overlayed.pdf") | |
puts "Overlay complete. Saved as 'overlayed.pdf'." | |
else | |
puts "The PDFs have a different number of pages and cannot be directly overlayed." | |
end | |
system("evince overlayed.pdf") | |
end | |
end | |
end | |
# Usage | |
session = MyCapybara::Session.new | |
session.login_and_download_pdf('your_username', 'your_password', 'http://127.0.0.1:3000/de/people/600351/membership.pdf', 'Mitgliederausweis Selfprint Vorschlag_13.05.24.pdf') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment