Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsSteensma committed Mar 26, 2024
2 parents 7b5014f + b447376 commit 193c21a
Show file tree
Hide file tree
Showing 10 changed files with 972 additions and 741 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/test.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
name: Test
on: [pull_request]
name: Build
on:
- push
- pull_request
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1.3
bundler-cache: true
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 16
node-version: 18
cache: 'npm'
cache-dependency-path: '**/package-lock.json'
- run: npm ci
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ jobs:
env:
GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: 3.1.3
bundler-cache: true
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 16
node-version: 18
- run: npm ci
- run: bundle exec rake spec
- run: gem build -o Dhalang.gem Dhalang.gemspec
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ build-iPhoneSimulator/

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc
.npmrc
52 changes: 52 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
PATH
remote: .
specs:
Dhalang (0.6.6)

GEM
remote: https://rubygems.org/
specs:
Ascii85 (1.1.0)
afm (0.2.2)
diff-lcs (1.5.0)
fastimage (2.2.6)
hashery (2.1.2)
pdf-reader (2.9.2)
Ascii85 (~> 1.0)
afm (~> 0.2.1)
hashery (~> 2.0)
ruby-rc4
ttfunk
rake (13.0.6)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
ruby-rc4 (0.1.5)
ttfunk (1.7.0)

PLATFORMS
arm64-darwin-21
x86-mingw32
x86_64-darwin-20
x86_64-linux

DEPENDENCIES
Dhalang!
bundler (~> 2.3.6)
fastimage (~> 2.2.6)
pdf-reader (~> 2.9.0)
rake (~> 13.0.6)
rspec (~> 3.0)

BUNDLED WITH
2.3.6
45 changes: 18 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Dhalang [![Build Status](https://travis-ci.com/NielsSteensma/Dhalang.svg?token=XZgKAByw2KZjcrsCh8gW&branch=master)](https://travis-ci.com/NielsSteensma/Dhalang)
# Dhalang [![Build](https://github.com/NielsSteensma/Dhalang/actions/workflows/build.yml/badge.svg)](https://github.com/NielsSteensma/Dhalang/actions/workflows/build.yml)

> Dhalang is a Ruby wrapper for Google's Puppeteer.


# Features
## Features
* Generate PDFs from pages
* Generate PDFs from html ( external images/stylesheets supported )
* Capture a screenshot of a webpage
* Scrape a webpage



# Installation
## Installation
Add this line to your application's Gemfile:

gem 'Dhalang'
Expand All @@ -25,47 +24,39 @@ Install puppeteer in your application's root directory:

$ npm install puppeteer

<sub>NodeJS v10.18.1 or greater is required</sub>
# Usage
### PDF
__Of a website url__
<sub>Dhalang and Puppeteer require Node ≥ 18 and Puppeteer ≥ 22</sub>
## Usage
__Get a PDF of a website url__
```ruby
Dhalang::PDF.get_from_url("https://www.google.com")
```
It is important to pass the complete url, leaving out https://, http:// or www. will result in an error.

__Of a HTML string__
__Get a PDF of a HTML string__
```ruby
Dhalang::PDF.get_from_html("<html><head></head><body><h1>examplestring</h1></body></html>")
```

*All methods return a string containing the PDF in binary.
*It is important to pass the complete url, leaving out https://, http:// or www. will result in an error.

### Screenshot

__PNG screenshot of a website__
__Get a PNG screenshot of a website__
```ruby
Dhalang::Screenshot.get_from_url("https://www.google.com", :png)
```

__JPEG screenshot of a website__
__Get a JPEG screenshot of a website__
```ruby
Dhalang::Screenshot.get_from_url("https://www.google.com", :jpeg)
```

__WEBP screenshot of a website__
__Get a WEBP screenshot of a website__
```ruby
Dhalang::Screenshot.get_from_url("https://www.google.com", :webp)
```
*All methods return a string containing the JPEG/PNG/WEBP in binary.

### Scraping

```ruby
Dhalang::Scraper.scrape("https://www.google.com")
```
All methods return a string containing the PDF or JPEG/PNG/WEBP in binary.


# Custom PDF/screenshot options

## Custom PDF/screenshot options
To override the default options that are set by Dhalang you can pass as last argument a hash with the custom options you want to set.

For example to set custom margins for PDFs:
Expand All @@ -90,7 +81,7 @@ A list of all possible screenshot options that can be set, can be found at: http
> For example: `Dhalang::PDF.get_from_url("https://www.google.com", {headerTemplateFile: '/tmp/header.html', footerTemplateFile: '/tmp/footer.html'})`

# Custom user options
## Custom user options
You may want to change the way Dhalang interacts with Puppeteer in general. User options can be set by providing them in a hash as last argument to any calls you make to the library. Are you setting both custom PDF and user options? Then they should be passed as a single hash.

For example to set a custom navigation timeout:
Expand All @@ -113,7 +104,7 @@ Below table lists all possible configuration parameters that can be set:
| chromeOptions | A array of [options](https://peter.sh/experiments/chromium-command-line-switches/) that can be passed to puppeteer in addition to the mandatory `['--no-sandbox', '--disable-setuid-sandbox']` | [] |


# Examples of using Dhalang
## Examples of using Dhalang
To return a PDF from a Rails controller you can do the following:
```ruby
def example_controller_method
Expand All @@ -128,4 +119,4 @@ def example_controller_method
binary_png = Dhalang::Screenshot.get_from_url("https://www.google.com", :png)
send_data(binary_png, filename: 'screenshotofgoogle.png', type: 'image/png')
end
```
```
2 changes: 1 addition & 1 deletion lib/Dhalang/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Dhalang
VERSION = "0.7.0"
VERSION = "0.6.6"
end
2 changes: 1 addition & 1 deletion lib/js/dhalang.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ exports.navigate = async function (page, configuration) {
} else if (navigationWaitForXPath !== "") {
await page.waitForXPath(navigationWaitForXPath, this.getWaitingParameters(configuration));
} else {
await page.waitForTimeout(250);
await new Promise(r => setTimeout(r, 250));
}
}

Expand Down
Loading

0 comments on commit 193c21a

Please sign in to comment.