html_to_pdf_conversion 0.1.3 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +164 -13
- data/example/example.rb +1 -2
- data/example/example_w_export.rb +23 -0
- data/lib/html_to_pdf_conversion/version.rb +1 -1
- data/lib/html_to_pdf_conversion.rb +23 -3
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86cc9418aec320a48946509abff35e245afdcd2b
|
4
|
+
data.tar.gz: 44e0fcccc79bac6d5bf2d60cddfd41e1f3a84542
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5620367347222840b48836b337809b068908fa0bdc53e03911d93089013c9b4b3076edb88e4b29f852744bff7efe0f7afc5125fd5aed96b1be16119e6d79d996
|
7
|
+
data.tar.gz: c3cc1e4f9d4733164cd7fa47d712cab72c86075e2bde425372b9a367e8dcd0a6973d8c25eba8cac0218750eeec27e28e6d45bddf61cbb9adedb95fe884446502
|
data/README.md
CHANGED
@@ -1,41 +1,192 @@
|
|
1
|
-
|
1
|
+
[![Travis](https://travis-ci.org/pmoelgaard/html_to_pdf_conversion.svg)](Travis)
|
2
2
|
|
3
|
-
|
3
|
+
# html_to_pdf_conversion
|
4
|
+
Node JavaScript wrapper for [the pdflayer API](https://pdflayer.com/).
|
5
|
+
Conversion API for Developers. Create highly customizable PDFs from URLs & HTML
|
4
6
|
|
5
|
-
|
7
|
+
---
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
9
11
|
Add this line to your application's Gemfile:
|
10
12
|
|
11
|
-
```
|
13
|
+
```
|
12
14
|
gem 'html_to_pdf_conversion'
|
15
|
+
|
13
16
|
```
|
14
17
|
|
15
18
|
And then execute:
|
16
19
|
|
17
|
-
|
20
|
+
```
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
```
|
18
24
|
|
19
25
|
Or install it yourself as:
|
20
26
|
|
21
|
-
|
27
|
+
```
|
28
|
+
$ gem install html_to_pdf_conversion
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
---
|
33
|
+
|
34
|
+
## Configuration
|
35
|
+
|
36
|
+
Before using the pdflayer API client you have to setup your account and obtain your API Access Key.
|
37
|
+
You can get it by signing up at [https://pdflayer.com/product](https://pdflayer.com/product).
|
38
|
+
|
39
|
+
---
|
40
|
+
|
41
|
+
## API Overview
|
42
|
+
All endpoints in the public API is available through this library.
|
43
|
+
|
44
|
+
- convert
|
45
|
+
|
46
|
+
---
|
22
47
|
|
23
48
|
## Usage
|
24
49
|
|
25
|
-
|
50
|
+
The general API is documented here: [https://pdflayer.com/documentation](https://pdflayer.com/documentation).
|
51
|
+
You can find parameters, result set definitions and status codes documented here as well.
|
52
|
+
|
53
|
+
In the examples directory you can find demos and samples of general usage of all the API functions.
|
54
|
+
|
55
|
+
### Setup
|
56
|
+
|
57
|
+
```
|
58
|
+
@client = PDFlayer::Client.new( [access_key], [secret_key] )
|
59
|
+
|
60
|
+
```
|
61
|
+
|
62
|
+
#### Required Parameters
|
63
|
+
|
64
|
+
###### access_key
|
65
|
+
Your unique key that you can find on the dashboard of your account under the pdflayer account.
|
66
|
+
|
67
|
+
###### secret_key
|
68
|
+
A userdefined private key that you can find on the dashboard of your account under the pdflayer account, it's used to encrypt parameters to prevent unauthorised usage of your account.
|
69
|
+
|
70
|
+
#### Optional Parameters
|
71
|
+
|
72
|
+
###### Secure (only available for Basic, Pro and Enterprise accounts)
|
73
|
+
Boolean value to indicate if the calls to the API should use a secure protocol or insecure (HTTP/HTTPS). Defaults to false (HTTP, insecure).
|
74
|
+
|
75
|
+
---
|
76
|
+
|
77
|
+
## Convert (simple case)
|
78
|
+
Takes a URL and returns the captured image.
|
79
|
+
|
80
|
+
###### Define Query
|
81
|
+
Second we define an options object.
|
82
|
+
All the options are documented here: [https://pdflayer.com/documentation](https://pdflayer.com/documentation)
|
83
|
+
|
84
|
+
```
|
85
|
+
options = PDFlayer::ConvertOptions.new()
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
###### Call Client
|
90
|
+
We then place the actual call to the API, passing in the URL we wish to capture as PDF and the options we defined above.
|
91
|
+
|
92
|
+
```
|
93
|
+
response = @client.convert(document_url, options)
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
###### Response
|
26
98
|
|
27
|
-
|
99
|
+
```
|
100
|
+
[The binary content of the PDF file]
|
101
|
+
|
102
|
+
```
|
103
|
+
|
104
|
+
---
|
105
|
+
|
106
|
+
## Convert (w. export)
|
107
|
+
Takes a URL, saves the PDF to a local file defined by the export argument and returns a response object.
|
108
|
+
|
109
|
+
###### Define Query
|
110
|
+
|
111
|
+
We define an options object, and sets the ```export``` option.
|
112
|
+
This option is specific to the RubyGem and not documented in the API found on pdflayer.
|
113
|
+
|
114
|
+
```
|
115
|
+
options = PDFlayer::ConvertOptions.new()
|
116
|
+
options.export = '[local_file_system]/some_directory]/my_file.pdf'
|
117
|
+
|
118
|
+
```
|
119
|
+
|
120
|
+
###### Simple Request (using Callback)
|
121
|
+
|
122
|
+
```
|
123
|
+
response = @client.convert(document_url, options)
|
124
|
+
|
125
|
+
```
|
126
|
+
|
127
|
+
###### Response
|
128
|
+
```
|
129
|
+
{
|
130
|
+
"success": true,
|
131
|
+
"info": "The PDF file has been saved to your local file system",
|
132
|
+
"file_name": "path_to_local_file.pdf"
|
133
|
+
}
|
134
|
+
```
|
135
|
+
|
136
|
+
---
|
28
137
|
|
29
|
-
|
138
|
+
## Example Application
|
30
139
|
|
31
|
-
|
140
|
+
In the [rootdir]/example directory there is a fully functional application which runs all requests against all the endpoints in the API, the examples above can be seen there as source code.
|
32
141
|
|
33
|
-
|
142
|
+
The example application uses a process.env variable to hold the access key.
|
34
143
|
|
35
|
-
|
144
|
+
---
|
36
145
|
|
146
|
+
## Tests
|
147
|
+
|
148
|
+
The tests are written for any NodeJS testing library, but has been run and targeted at the [https://mochajs.org/](https://mochajs.org/) testing library.
|
149
|
+
|
150
|
+
In order to run the tests, the following environment variables needs to be set:
|
151
|
+
|
152
|
+
```
|
153
|
+
ACCESS_KEY = [access_key]
|
154
|
+
SECRET_KEY = [secret_key]
|
155
|
+
```
|
156
|
+
|
157
|
+
|
158
|
+
---
|
159
|
+
|
160
|
+
## Customer Support
|
161
|
+
|
162
|
+
Need any assistance? [Get in touch with Customer Support](mailto:support@apilayer.net?subject=%pdflayer%5D).
|
163
|
+
|
164
|
+
---
|
165
|
+
|
166
|
+
## Updates
|
167
|
+
Stay up to date by following [@apilayernet](https://twitter.com/apilayernet) on Twitter.
|
168
|
+
|
169
|
+
---
|
170
|
+
|
171
|
+
## Legal
|
172
|
+
|
173
|
+
All usage of the languagelayer website, API, and services is subject to the [pdflayer Terms & Conditions](https://pdflayer.com/terms) and all annexed legal documents and agreements.
|
174
|
+
|
175
|
+
---
|
176
|
+
|
177
|
+
## Author
|
178
|
+
Peter Andreas Moelgaard ([GitHub](https://github.com/pmoelgaard), [Twitter](https://twitter.com/petermoelgaard))
|
179
|
+
|
180
|
+
---
|
37
181
|
|
38
182
|
## License
|
183
|
+
Licensed under the The MIT License (MIT)
|
184
|
+
|
185
|
+
Copyright (©) 2016 Peter Andreas Moelgaard & apilayer
|
186
|
+
|
187
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
188
|
+
|
189
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
39
190
|
|
40
|
-
|
191
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
41
192
|
|
data/example/example.rb
CHANGED
@@ -12,12 +12,11 @@ document_url = 'https://en.wikipedia.org/wiki/Special:Random'
|
|
12
12
|
|
13
13
|
# We declare the options
|
14
14
|
options = PDFlayer::ConvertOptions.new()
|
15
|
-
options.export = File.join('tmp', SecureRandom.uuid() +'.pdf')
|
16
15
|
|
17
16
|
# We make the call to convert
|
18
17
|
response = @client.convert(document_url, options)
|
19
18
|
|
20
19
|
# If its a success, we print a message to the user
|
21
20
|
if ! response.nil?
|
22
|
-
puts 'SUCCESS : PDF fetched...'
|
21
|
+
puts 'SUCCESS : PDF fetched...'+ response.length
|
23
22
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'html_to_pdf_conversion'
|
2
|
+
require 'dotenv'
|
3
|
+
|
4
|
+
# Load Environment Variables
|
5
|
+
Dotenv.load
|
6
|
+
|
7
|
+
# Declare the Client instance passing in the authentication parameters
|
8
|
+
@client = PDFlayer::Client.new(ENV['ACCESS_KEY'], ENV['SECRET_KEY'])
|
9
|
+
|
10
|
+
# Set the URL to get as PDF, we take a random URL from Wikipedia
|
11
|
+
document_url = 'https://en.wikipedia.org/wiki/Special:Random'
|
12
|
+
|
13
|
+
# We declare the options
|
14
|
+
options = PDFlayer::ConvertOptions.new()
|
15
|
+
options.export = File.join('tmp', SecureRandom.uuid() +'.pdf')
|
16
|
+
|
17
|
+
# We make the call to convert
|
18
|
+
response = @client.convert(document_url, options)
|
19
|
+
|
20
|
+
# If its a success, we print a message to the user
|
21
|
+
if ! response.nil?
|
22
|
+
puts 'SUCCESS : PDF fetched...'+ JSON(response)
|
23
|
+
end
|
@@ -30,6 +30,11 @@ module PDFlayer
|
|
30
30
|
|
31
31
|
def convert(document_url, options = {})
|
32
32
|
|
33
|
+
if document_url.nil?
|
34
|
+
raise PDFlayer::MissingArgumentException.new 'document_url'
|
35
|
+
return
|
36
|
+
end
|
37
|
+
|
33
38
|
# Create a shallow copy so we don't manipulate the original reference
|
34
39
|
query = options.dup
|
35
40
|
|
@@ -58,16 +63,31 @@ module PDFlayer
|
|
58
63
|
res.inspect
|
59
64
|
|
60
65
|
# If we have an export option passed in, we save it to local file system
|
61
|
-
if
|
66
|
+
if options.export.nil?
|
67
|
+
|
68
|
+
# We just return the parsed binary response
|
69
|
+
return res.parsed_response
|
70
|
+
|
71
|
+
else
|
72
|
+
|
62
73
|
begin
|
74
|
+
|
63
75
|
File.open(options.export, 'a+') do |file|
|
76
|
+
|
64
77
|
file.write(res.body)
|
78
|
+
|
79
|
+
result = {
|
80
|
+
success: true,
|
81
|
+
info: "The PDF file has been saved to your local file system",
|
82
|
+
file_name: options.export
|
83
|
+
}
|
84
|
+
return result
|
85
|
+
|
65
86
|
end
|
87
|
+
|
66
88
|
end
|
67
89
|
end
|
68
90
|
|
69
|
-
# Finally we return the parsed binary response
|
70
|
-
return res.parsed_response
|
71
91
|
|
72
92
|
rescue => e
|
73
93
|
puts e.inspect
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html_to_pdf_conversion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Andreas Moelgaard
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- bin/console
|
126
126
|
- bin/setup
|
127
127
|
- example/example.rb
|
128
|
+
- example/example_w_export.rb
|
128
129
|
- html_to_pdf_conversion.gemspec
|
129
130
|
- html_to_pdf_conversion.iml
|
130
131
|
- lib/html_to_pdf_conversion.rb
|