majorsilence_pdf 26.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f637080b24cfad64802eb216c50e3f8463acfc427eda1a12e6a7d444a2b4f89f
4
+ data.tar.gz: 150e892607e4e5c597e4f76665aad2331a5f430db2d34c4a15fbb61672ffac97
5
+ SHA512:
6
+ metadata.gz: 9ead34ffe65cd329fd1197a9f5dc87e9c24cd4b61eaa1b69a37fa3823ef2af7dae71e243c97141df9238e5368518614492878a85b957b5d58c17c13c9b37ad94
7
+ data.tar.gz: d24a91f76b546da5dd904e1a3db49ce9e8dc0c5ffa0dbf3d8b0e59e6cd513f62f1e83a1058e889daca75bb66c427a9d017809f8af76a18f333e7f13eca11d211
data/LICENSE ADDED
@@ -0,0 +1,5 @@
1
+ This software is available under your choice of the following licenses:
2
+
3
+ MIT License — see LICENSE-MIT
4
+ Apache License 2.0 — see LICENSE-APACHE
5
+ BSD 2-Clause License — see LICENSE-BSD
data/LICENSE-APACHE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright (C) 2026 Peter Gill <peter@majorsilence.com>
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/LICENSE-BSD ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (C) 2026 Peter Gill <peter@majorsilence.com>
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
7
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/LICENSE-MIT ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (C) 2026 Peter Gill <peter@majorsilence.com>
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
data/README.md ADDED
@@ -0,0 +1,175 @@
1
+ # Majorsilence PDF — Ruby Wrapper
2
+
3
+ Ruby bindings for [Majorsilence PDF](https://github.com/majorsilence/Reporting) — generate PDF documents in-process via an AOT-compiled native shared library (`pdfnative`), accessed through Fiddle FFI. No subprocess is spawned and no .NET runtime is required at runtime.
4
+
5
+ **Ruby 3.0 or later** is recommended (`# frozen_string_literal: true` and endless method definitions are used throughout; `Fiddle` is part of the standard library through Ruby 3.x).
6
+
7
+ ## Features
8
+
9
+ - Text with font size, bold, italic, underline, strikethrough, colour, and alignment
10
+ - Shapes: rectangles, lines, circles, and custom strokes
11
+ - Images (embedded or from disk)
12
+ - Tables with header colours, alternating row colours, borders, and cell padding
13
+ - Multi-page documents
14
+ - Custom and system fonts, Unicode / RTL text
15
+ - Password-protected PDFs with fine-grained permission flags
16
+ - PDF merging
17
+ - Annotations and hyperlinks
18
+ - Page sizes: A3, A4, A5, Letter, Legal, Tabloid
19
+
20
+ ---
21
+
22
+ ## Installation
23
+
24
+ Install from [RubyGems](https://rubygems.org/gems/majorsilence_pdf):
25
+
26
+ ```bash
27
+ gem install majorsilence_pdf
28
+ ```
29
+
30
+ Or add it to your `Gemfile`:
31
+
32
+ ```ruby
33
+ gem 'majorsilence_pdf'
34
+ ```
35
+
36
+ The gem itself has no dependency beyond `fiddle` (Ruby's standard FFI library). You still need the `pdfnative` shared library, described below.
37
+
38
+ ### Install Ruby
39
+
40
+ - **Linux (Debian/Ubuntu):**
41
+ ```bash
42
+ sudo apt-get update && sudo apt-get install -y ruby
43
+ ruby --version
44
+ ```
45
+ - **macOS** (via Homebrew):
46
+ ```bash
47
+ brew install ruby
48
+ ruby --version
49
+ ```
50
+ - **Windows** — download the installer from [rubyinstaller.org](https://rubyinstaller.org/) or use winget:
51
+ ```powershell
52
+ winget install RubyInstallerTeam.Ruby
53
+ ruby --version
54
+ ```
55
+
56
+ ### Get the pdfnative shared library
57
+
58
+ Download the native library from a [Majorsilence Reporting release](https://github.com/majorsilence/Reporting/releases) (`majorsilence-pdf-pdfnative-linux.zip`, `-osx.zip`, or `-windows.zip`) and extract it. The directory contains the shared library and all its sibling libraries.
59
+
60
+ | Platform | Library filename |
61
+ |---|---|
62
+ | Linux | `libpdfnative.so` |
63
+ | macOS | `libpdfnative.dylib` |
64
+ | Windows | `pdfnative.dll` |
65
+
66
+ ---
67
+
68
+ ## Quick start
69
+
70
+ ```ruby
71
+ require 'majorsilence_pdf'
72
+
73
+ lib = PdfLibrary.load('/path/to/libpdfnative.so')
74
+
75
+ doc = PdfDocument.new(lib)
76
+ doc.set_title('Hello World')
77
+ doc.set_author('My App')
78
+
79
+ canvas = doc.add_page(*PAGE_A4)
80
+
81
+ heading = PdfStyle.new(lib)
82
+ heading.set_size(24).set_bold
83
+ canvas.draw_text('Hello, PDF!', 72, 80, heading.handle)
84
+ heading.close
85
+
86
+ body = PdfStyle.new(lib)
87
+ body.set_size(12)
88
+ canvas.draw_text('Created with majorsilence_pdf -- no .NET runtime required.', 72, 120, body.handle)
89
+ body.close
90
+
91
+ canvas.close
92
+ doc.save('hello.pdf')
93
+ doc.close
94
+ ```
95
+
96
+ ## Tables
97
+
98
+ ```ruby
99
+ require 'majorsilence_pdf'
100
+
101
+ lib = PdfLibrary.load('/path/to/libpdfnative.so')
102
+
103
+ doc = PdfDocument.new(lib)
104
+ canvas = doc.add_page(*PAGE_A4)
105
+
106
+ table = PdfTable.new(lib, [180, 80, 90, 90])
107
+ table.set_header_bg(26, 86, 160)
108
+ .set_alternate_bg(240, 245, 252)
109
+ .set_border(200, 200, 200, 0.5)
110
+ .set_cell_padding(5)
111
+ .add_row('Product', 'Qty', 'Unit Price', 'Total')
112
+ .add_row('PDF Library Pro', '3', '$400.00', '$1,200.00')
113
+ .add_row('Report Designer', '1', '$250.00', '$250.00')
114
+
115
+ canvas.draw_table(table.handle, 72, 92)
116
+ table.close
117
+
118
+ canvas.close
119
+ doc.save('report.pdf')
120
+ doc.close
121
+ ```
122
+
123
+ Or write to memory instead of a file with `doc.save_to_memory`, which returns the PDF as a binary `String`.
124
+
125
+ On Linux, pre-load the library directory before starting Ruby:
126
+
127
+ ```bash
128
+ export LD_LIBRARY_PATH=/path/to/pdfnative-dir:$LD_LIBRARY_PATH
129
+ ruby your_script.rb
130
+ ```
131
+
132
+ ---
133
+
134
+ ## More examples
135
+
136
+ See the [`Examples/`](Examples) directory for 18 ready-to-run scripts covering text styles, shapes, images, custom fonts, Unicode/RTL text, invoices, dashboards, password protection, table layout, and PDF merging. Each expects `PDFNATIVE_LIB` set to the path of the shared library:
137
+
138
+ ```bash
139
+ PDFNATIVE_LIB=/path/to/libpdfnative.so ruby Examples/example_01_hello_world.rb
140
+ ```
141
+
142
+ ---
143
+
144
+ ## Releasing (maintainers)
145
+
146
+ Releases are published to [RubyGems](https://rubygems.org/gems/majorsilence_pdf) by `.github/workflows/publish.yml` using [Trusted Publishing](https://guides.rubygems.org/trusted-publishing/) (OIDC) -- no API key is stored in this repo.
147
+
148
+ One-time setup on rubygems.org, under Profile -> Trusted publishers (or "Pending trusted publishers" before the gem's first release):
149
+
150
+ | Field | Value |
151
+ |---|---|
152
+ | Gem name | `majorsilence_pdf` |
153
+ | Repository owner | `majorsilence` |
154
+ | Repository name | `pdf-ruby` |
155
+ | Workflow filename | `publish.yml` |
156
+ | Environment | `release` |
157
+
158
+ To cut a release:
159
+
160
+ ```bash
161
+ # Bump the version
162
+ $EDITOR lib/majorsilence_pdf/version.rb
163
+
164
+ git commit -am "Release vX.Y.Z"
165
+ git tag vX.Y.Z
166
+ git push origin main --tags
167
+ ```
168
+
169
+ Pushing the `vX.Y.Z` tag triggers the workflow, which runs the test suite and then uses [`rubygems/release-gem`](https://github.com/rubygems/release-gem) to build and push the gem.
170
+
171
+ ---
172
+
173
+ ## License
174
+
175
+ Triple-licensed under [MIT](LICENSE-MIT), [Apache-2.0](LICENSE-APACHE), and [BSD-2-Clause](LICENSE-BSD). Choose whichever suits your project.
@@ -0,0 +1,466 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fiddle'
4
+ require 'fiddle/import'
5
+ require 'tempfile'
6
+
7
+ # Ruby Fiddle wrapper for the pdfnative shared library.
8
+ #
9
+ # Loads the Majorsilence PDF engine in-process via Fiddle — no subprocess
10
+ # is spawned, no .NET runtime is required on the host.
11
+ #
12
+ # Platform-specific library filenames:
13
+ # Linux: libpdfnative.so
14
+ # macOS: libpdfnative.dylib
15
+ # Windows: pdfnative.dll
16
+ #
17
+ # Usage:
18
+ # require 'majorsilence_pdf/pdf_native'
19
+ #
20
+ # lib = PdfLibrary.load('/path/to/libpdfnative.so')
21
+ #
22
+ # doc = PdfDocument.new(lib)
23
+ # doc.set_title('My Report')
24
+ # canvas = doc.add_page(595.28, 841.89)
25
+ # canvas.draw_text('Hello, PDF!', 72, 100)
26
+ # canvas.close
27
+ # doc.save('/tmp/output.pdf')
28
+ # doc.close
29
+ #
30
+ # Page sizes (points):
31
+ # A4 = [595.28, 841.89] Letter = [612.0, 792.0]
32
+ # A3 = [841.89, 1190.55] Legal = [612.0, 1008.0]
33
+ # A5 = [419.53, 595.28] Tabloid= [792.0, 1224.0]
34
+
35
+ # ── Page size constants ────────────────────────────────────────────────────────
36
+
37
+ PAGE_A4 = [595.28, 841.89].freeze
38
+ PAGE_A3 = [841.89, 1190.55].freeze
39
+ PAGE_A5 = [419.53, 595.28].freeze
40
+ PAGE_LETTER = [612.0, 792.0].freeze
41
+ PAGE_LEGAL = [612.0, 1008.0].freeze
42
+ PAGE_TABLOID = [792.0, 1224.0].freeze
43
+
44
+ # ── Style alignment constants ──────────────────────────────────────────────────
45
+
46
+ ALIGN_LEFT = 0
47
+ ALIGN_CENTER = 1
48
+ ALIGN_RIGHT = 2
49
+
50
+ # ── Text decoration constants ──────────────────────────────────────────────────
51
+
52
+ DECOR_NONE = 0
53
+ DECOR_UNDERLINE = 1
54
+ DECOR_STRIKETHROUGH = 2
55
+ DECOR_OVERLINE = 3
56
+
57
+ # ── Encryption permission flags ───────────────────────────────────────────────
58
+
59
+ PERM_PRINT = 4
60
+ PERM_MODIFY_CONTENT = 8
61
+ PERM_COPY_TEXT = 16
62
+ PERM_ADD_ANNOTATIONS = 32
63
+ PERM_FILL_FORMS = 256
64
+ PERM_EXTRACT_TEXT = 512
65
+ PERM_ASSEMBLE = 1024
66
+ PERM_PRINT_HIGH_QUALITY = 2048
67
+ PERM_ALL = -1
68
+
69
+ module PdfLibrary
70
+ # Load the pdfnative shared library and initialize the engine.
71
+ # Returns a Hash of bound Fiddle::Function objects.
72
+ # Call this once per process before creating any PdfDocument instances.
73
+ def self.load(lib_path)
74
+ lib_path = File.expand_path(lib_path)
75
+ lib_dir = File.dirname(lib_path)
76
+
77
+ ENV['PDFNATIVE_LIB_DIR'] = lib_dir
78
+
79
+ # Pre-load all shared libraries in the directory with RTLD_GLOBAL so that
80
+ # .NET 10+ runtime components are globally visible.
81
+ ext = RUBY_PLATFORM =~ /darwin/ ? '*.dylib' : '*.so'
82
+ Dir.glob(File.join(lib_dir, ext)).sort.each do |f|
83
+ begin Fiddle.dlopen(f) rescue Fiddle::DLError; end
84
+ end
85
+
86
+ handle = Fiddle.dlopen(lib_path)
87
+
88
+ fns = {
89
+ pdf_init: Fiddle::Function.new(handle['pdf_init'], [], Fiddle::TYPE_INT),
90
+
91
+ pdf_doc_create: Fiddle::Function.new(handle['pdf_doc_create'], [], Fiddle::TYPE_VOIDP),
92
+ pdf_doc_set_title: Fiddle::Function.new(handle['pdf_doc_set_title'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
93
+ pdf_doc_set_author: Fiddle::Function.new(handle['pdf_doc_set_author'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
94
+ pdf_doc_set_subject: Fiddle::Function.new(handle['pdf_doc_set_subject'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
95
+ pdf_doc_set_creator: Fiddle::Function.new(handle['pdf_doc_set_creator'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
96
+ pdf_doc_set_security: Fiddle::Function.new(handle['pdf_doc_set_security'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
97
+ pdf_doc_add_page: Fiddle::Function.new(handle['pdf_doc_add_page'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT], Fiddle::TYPE_VOIDP),
98
+ pdf_doc_save_file: Fiddle::Function.new(handle['pdf_doc_save_file'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
99
+ pdf_doc_close: Fiddle::Function.new(handle['pdf_doc_close'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOID),
100
+
101
+ pdf_canvas_draw_text: Fiddle::Function.new(handle['pdf_canvas_draw_text'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
102
+ pdf_canvas_draw_textbox: Fiddle::Function.new(handle['pdf_canvas_draw_textbox'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT], Fiddle::TYPE_INT),
103
+ pdf_canvas_draw_line: Fiddle::Function.new(handle['pdf_canvas_draw_line'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_FLOAT], Fiddle::TYPE_INT),
104
+ pdf_canvas_draw_rect: Fiddle::Function.new(handle['pdf_canvas_draw_rect'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
105
+ pdf_canvas_draw_ellipse: Fiddle::Function.new(handle['pdf_canvas_draw_ellipse'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
106
+ pdf_canvas_draw_table: Fiddle::Function.new(handle['pdf_canvas_draw_table'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT], Fiddle::TYPE_INT),
107
+ pdf_canvas_add_link: Fiddle::Function.new(handle['pdf_canvas_add_link'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_FLOAT, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
108
+ pdf_canvas_close: Fiddle::Function.new(handle['pdf_canvas_close'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOID),
109
+
110
+ pdf_style_create: Fiddle::Function.new(handle['pdf_style_create'], [], Fiddle::TYPE_VOIDP),
111
+ pdf_style_set_font_family:Fiddle::Function.new(handle['pdf_style_set_font_family'],[Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
112
+ pdf_style_set_font_file: Fiddle::Function.new(handle['pdf_style_set_font_file'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
113
+ pdf_style_set_size: Fiddle::Function.new(handle['pdf_style_set_size'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT], Fiddle::TYPE_INT),
114
+ pdf_style_set_color: Fiddle::Function.new(handle['pdf_style_set_color'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
115
+ pdf_style_set_bold: Fiddle::Function.new(handle['pdf_style_set_bold'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
116
+ pdf_style_set_italic: Fiddle::Function.new(handle['pdf_style_set_italic'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
117
+ pdf_style_set_alignment: Fiddle::Function.new(handle['pdf_style_set_alignment'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
118
+ pdf_style_set_decoration: Fiddle::Function.new(handle['pdf_style_set_decoration'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
119
+ pdf_style_close: Fiddle::Function.new(handle['pdf_style_close'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOID),
120
+
121
+ pdf_table_create: Fiddle::Function.new(handle['pdf_table_create'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_VOIDP),
122
+ pdf_table_set_header_bg: Fiddle::Function.new(handle['pdf_table_set_header_bg'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
123
+ pdf_table_set_alternate_bg:Fiddle::Function.new(handle['pdf_table_set_alternate_bg'],[Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
124
+ pdf_table_set_border: Fiddle::Function.new(handle['pdf_table_set_border'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_INT, Fiddle::TYPE_FLOAT], Fiddle::TYPE_INT),
125
+ pdf_table_set_cell_padding:Fiddle::Function.new(handle['pdf_table_set_cell_padding'],[Fiddle::TYPE_VOIDP, Fiddle::TYPE_FLOAT], Fiddle::TYPE_INT),
126
+ pdf_table_stage_cell: Fiddle::Function.new(handle['pdf_table_stage_cell'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
127
+ pdf_table_commit_row: Fiddle::Function.new(handle['pdf_table_commit_row'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
128
+ pdf_table_close: Fiddle::Function.new(handle['pdf_table_close'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOID),
129
+
130
+ pdf_merge_create: Fiddle::Function.new(handle['pdf_merge_create'], [], Fiddle::TYPE_VOIDP),
131
+ pdf_merge_add_bytes: Fiddle::Function.new(handle['pdf_merge_add_bytes'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_INT], Fiddle::TYPE_INT),
132
+ pdf_merge_save_file: Fiddle::Function.new(handle['pdf_merge_save_file'], [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT),
133
+ pdf_merge_close: Fiddle::Function.new(handle['pdf_merge_close'], [Fiddle::TYPE_VOIDP], Fiddle::TYPE_VOID),
134
+
135
+ pdf_last_error: Fiddle::Function.new(handle['pdf_last_error'], [], Fiddle::TYPE_VOIDP),
136
+ }
137
+
138
+ ret = fns[:pdf_init].call
139
+ raise "pdf_init failed: #{last_error_str(fns)}" unless ret.zero?
140
+
141
+ fns
142
+ end
143
+
144
+ def self.last_error_str(fns)
145
+ ptr = fns[:pdf_last_error].call
146
+ return 'unknown error' if ptr.nil? || ptr.zero?
147
+
148
+ Fiddle::Pointer.new(ptr).to_s
149
+ end
150
+ end
151
+
152
+ class PdfDocument
153
+ def initialize(fns)
154
+ @fns = fns
155
+ h = fns[:pdf_doc_create].call
156
+ raise "pdf_doc_create failed: #{last_error}" if h.nil? || h.zero?
157
+
158
+ @handle = h
159
+ end
160
+
161
+ def set_title(title) = check(@fns[:pdf_doc_set_title].call(@handle, c_str(title)), 'pdf_doc_set_title')
162
+ def set_author(author) = check(@fns[:pdf_doc_set_author].call(@handle, c_str(author)), 'pdf_doc_set_author')
163
+ def set_subject(subject)= check(@fns[:pdf_doc_set_subject].call(@handle, c_str(subject)), 'pdf_doc_set_subject')
164
+ def set_creator(creator)= check(@fns[:pdf_doc_set_creator].call(@handle, c_str(creator)), 'pdf_doc_set_creator')
165
+
166
+ # Apply password-based AES encryption.
167
+ # user_password - password to open; empty string = no open password
168
+ # owner_password - full-control password; nil = same as user_password
169
+ # permissions - bitmask of PERM_* constants; -1 = allow all
170
+ # aes256 - true = AES-256 (default), false = AES-128
171
+ def set_security(user_password: '', owner_password: nil, permissions: PERM_ALL, aes256: true)
172
+ enc_version = aes256 ? 0 : 1
173
+ owner_ptr = owner_password ? c_str(owner_password) : Fiddle::NULL
174
+ check(
175
+ @fns[:pdf_doc_set_security].call(@handle, c_str(user_password), owner_ptr, permissions, enc_version),
176
+ 'pdf_doc_set_security'
177
+ )
178
+ self
179
+ end
180
+
181
+ # Add a page and return a PdfCanvas for drawing.
182
+ # width / height in points. A4 = 595.28 x 841.89.
183
+ def add_page(width = 595.28, height = 841.89)
184
+ h = @fns[:pdf_doc_add_page].call(@handle, width.to_f, height.to_f)
185
+ raise "pdf_doc_add_page failed: #{last_error}" if h.nil? || h.zero?
186
+
187
+ PdfCanvas.new(@fns, h)
188
+ end
189
+
190
+ # Write the completed document to path.
191
+ def save(path)
192
+ check(@fns[:pdf_doc_save_file].call(@handle, c_str(path)), 'pdf_doc_save_file')
193
+ end
194
+
195
+ # Write to a temp file and return the PDF bytes.
196
+ def save_to_memory
197
+ tmp = Tempfile.new(['pdfnative', '.pdf'], binmode: true)
198
+ tmp_path = tmp.path
199
+ tmp.close
200
+ begin
201
+ save(tmp_path)
202
+ File.binread(tmp_path)
203
+ ensure
204
+ File.delete(tmp_path) if File.exist?(tmp_path)
205
+ end
206
+ end
207
+
208
+ def close
209
+ return unless @handle
210
+
211
+ @fns[:pdf_doc_close].call(@handle)
212
+ @handle = nil
213
+ end
214
+
215
+ private
216
+
217
+ def c_str(str) = Fiddle::Pointer[str.encode('UTF-8') + "\0"]
218
+
219
+ def last_error
220
+ PdfLibrary.last_error_str(@fns)
221
+ end
222
+
223
+ def check(ret, fn)
224
+ raise "#{fn} failed: #{last_error}" unless ret.zero?
225
+
226
+ self
227
+ end
228
+ end
229
+
230
+ class PdfCanvas
231
+ def initialize(fns, handle)
232
+ @fns = fns
233
+ @handle = handle
234
+ end
235
+
236
+ # Draw text with its baseline at (x, y).
237
+ # style_handle is the raw handle from PdfStyle#handle, or nil for default.
238
+ def draw_text(text, x, y, style_handle = nil)
239
+ style_ptr = style_handle || Fiddle::NULL
240
+ check(@fns[:pdf_canvas_draw_text].call(@handle, c_str(text), x.to_f, y.to_f, style_ptr), 'pdf_canvas_draw_text')
241
+ end
242
+
243
+ # Draw word-wrapped text in a box. Returns overflow char offset.
244
+ def draw_textbox(text, x, y, width, height, style_handle = nil, line_spacing = 0.0)
245
+ style_ptr = style_handle || Fiddle::NULL
246
+ ret = @fns[:pdf_canvas_draw_textbox].call(@handle, c_str(text), x.to_f, y.to_f, width.to_f, height.to_f, style_ptr, line_spacing.to_f)
247
+ raise "pdf_canvas_draw_textbox failed: #{last_error}" if ret < 0
248
+
249
+ ret
250
+ end
251
+
252
+ # Draw a line from (x1,y1) to (x2,y2). r, g, b are 0-255.
253
+ def draw_line(x1, y1, x2, y2, r: 0, g: 0, b: 0, width: 1.0)
254
+ check(@fns[:pdf_canvas_draw_line].call(@handle, x1.to_f, y1.to_f, x2.to_f, y2.to_f, r, g, b, width.to_f), 'pdf_canvas_draw_line')
255
+ end
256
+
257
+ # Draw a rectangle. fill_rgb and stroke_rgb are [r, g, b] arrays or nil.
258
+ def draw_rect(x, y, width, height, fill_rgb: nil, stroke_rgb: nil, stroke_width: 1.0)
259
+ fr, fg, fb = fill_rgb || [0, 0, 0]
260
+ sr, sg, sb = stroke_rgb || [0, 0, 0]
261
+ check(
262
+ @fns[:pdf_canvas_draw_rect].call(
263
+ @handle, x.to_f, y.to_f, width.to_f, height.to_f,
264
+ fr, fg, fb, fill_rgb ? 1 : 0,
265
+ sr, sg, sb, stroke_width.to_f, stroke_rgb ? 1 : 0
266
+ ),
267
+ 'pdf_canvas_draw_rect'
268
+ )
269
+ end
270
+
271
+ # Draw an ellipse bounded by the given rectangle.
272
+ def draw_ellipse(x, y, width, height, fill_rgb: nil, stroke_rgb: nil, stroke_width: 1.0)
273
+ fr, fg, fb = fill_rgb || [0, 0, 0]
274
+ sr, sg, sb = stroke_rgb || [0, 0, 0]
275
+ check(
276
+ @fns[:pdf_canvas_draw_ellipse].call(
277
+ @handle, x.to_f, y.to_f, width.to_f, height.to_f,
278
+ fr, fg, fb, fill_rgb ? 1 : 0,
279
+ sr, sg, sb, stroke_width.to_f, stroke_rgb ? 1 : 0
280
+ ),
281
+ 'pdf_canvas_draw_ellipse'
282
+ )
283
+ end
284
+
285
+ # Draw a PdfTable at (x, y). Pass the table's handle via table.handle.
286
+ def draw_table(table_handle, x, y)
287
+ check(@fns[:pdf_canvas_draw_table].call(@handle, table_handle, x.to_f, y.to_f), 'pdf_canvas_draw_table')
288
+ end
289
+
290
+ # Add a clickable hyperlink over the given rectangle.
291
+ def add_link(x, y, width, height, uri)
292
+ check(@fns[:pdf_canvas_add_link].call(@handle, x.to_f, y.to_f, width.to_f, height.to_f, c_str(uri)), 'pdf_canvas_add_link')
293
+ end
294
+
295
+ def close
296
+ return unless @handle
297
+
298
+ @fns[:pdf_canvas_close].call(@handle)
299
+ @handle = nil
300
+ end
301
+
302
+ # Expose the raw handle so PdfDocument#add_page callers can pass it to draw_table.
303
+ def handle = @handle
304
+
305
+ private
306
+
307
+ def c_str(str) = Fiddle::Pointer[str.encode('UTF-8') + "\0"]
308
+
309
+ def last_error = PdfLibrary.last_error_str(@fns)
310
+
311
+ def check(ret, fn)
312
+ raise "#{fn} failed: #{last_error}" unless ret.zero?
313
+
314
+ self
315
+ end
316
+ end
317
+
318
+ # A text style handle. Defaults: Helvetica 12 pt black left-aligned.
319
+ # Call close when done.
320
+ class PdfStyle
321
+ attr_reader :handle
322
+
323
+ def initialize(fns)
324
+ @fns = fns
325
+ h = fns[:pdf_style_create].call
326
+ raise "pdf_style_create failed: #{PdfLibrary.last_error_str(fns)}" if h.nil? || h.zero?
327
+
328
+ @handle = h
329
+ end
330
+
331
+ def set_font_family(family) = tap { @fns[:pdf_style_set_font_family].call(@handle, c_str(family)) }
332
+ def set_font_file(path) = tap { @fns[:pdf_style_set_font_file].call(@handle, c_str(path)) }
333
+ def set_size(points) = tap { @fns[:pdf_style_set_size].call(@handle, points.to_f) }
334
+ def set_color(r, g, b) = tap { @fns[:pdf_style_set_color].call(@handle, r, g, b) }
335
+ def set_bold(bold = true) = tap { @fns[:pdf_style_set_bold].call(@handle, bold ? 1 : 0) }
336
+ def set_italic(ital = true) = tap { @fns[:pdf_style_set_italic].call(@handle, ital ? 1 : 0) }
337
+
338
+ # alignment: 0 = left, 1 = center, 2 = right. Use ALIGN_* constants.
339
+ def set_alignment(alignment) = tap { @fns[:pdf_style_set_alignment].call(@handle, alignment) }
340
+
341
+ # decoration: 0 = none, 1 = underline, 2 = strikethrough, 3 = overline. Use DECOR_* constants.
342
+ def set_decoration(decoration) = tap { @fns[:pdf_style_set_decoration].call(@handle, decoration) }
343
+
344
+ def close
345
+ return unless @handle
346
+
347
+ @fns[:pdf_style_close].call(@handle)
348
+ @handle = nil
349
+ end
350
+
351
+ private
352
+
353
+ def c_str(str) = Fiddle::Pointer[str.encode('UTF-8') + "\0"]
354
+ end
355
+
356
+ # A table layout handle. Call close when done.
357
+ #
358
+ # Usage:
359
+ # table = PdfTable.new(fns, [180, 80, 90, 90])
360
+ # table.set_header_bg(26, 86, 160)
361
+ # .set_alternate_bg(240, 245, 252)
362
+ # .set_border(200, 200, 200, 0.5)
363
+ # .add_row('Product', 'Qty', 'Unit Price', 'Total')
364
+ # .add_row('Widget', '3', '$10.00', '$30.00')
365
+ # canvas.draw_table(table.handle, 72, 100)
366
+ # table.close
367
+ class PdfTable
368
+ attr_reader :handle
369
+
370
+ def initialize(fns, col_widths)
371
+ @fns = fns
372
+ # Pack column widths as a native float array.
373
+ packed = col_widths.map(&:to_f).pack('f*')
374
+ ptr = Fiddle::Pointer[packed]
375
+ h = fns[:pdf_table_create].call(ptr, col_widths.length)
376
+ raise "pdf_table_create failed: #{PdfLibrary.last_error_str(fns)}" if h.nil? || h.zero?
377
+
378
+ @handle = h
379
+ # Keep packed alive so the pointer remains valid during the call.
380
+ @_col_widths_packed = packed
381
+ end
382
+
383
+ def set_header_bg(r, g, b) = tap { @fns[:pdf_table_set_header_bg].call(@handle, r, g, b) }
384
+ def set_alternate_bg(r, g, b)= tap { @fns[:pdf_table_set_alternate_bg].call(@handle, r, g, b) }
385
+ def set_border(r, g, b, w) = tap { @fns[:pdf_table_set_border].call(@handle, r, g, b, w.to_f) }
386
+ def set_cell_padding(padding) = tap { @fns[:pdf_table_set_cell_padding].call(@handle, padding.to_f) }
387
+
388
+ # Stage all cells and commit them as a single row.
389
+ def add_row(*cells)
390
+ cells.each do |cell|
391
+ ret = @fns[:pdf_table_stage_cell].call(@handle, c_str(cell.to_s))
392
+ raise "pdf_table_stage_cell failed: #{PdfLibrary.last_error_str(@fns)}" unless ret.zero?
393
+ end
394
+ ret = @fns[:pdf_table_commit_row].call(@handle)
395
+ raise "pdf_table_commit_row failed: #{PdfLibrary.last_error_str(@fns)}" unless ret.zero?
396
+
397
+ self
398
+ end
399
+
400
+ def close
401
+ return unless @handle
402
+
403
+ @fns[:pdf_table_close].call(@handle)
404
+ @handle = nil
405
+ end
406
+
407
+ private
408
+
409
+ def c_str(str) = Fiddle::Pointer[str.encode('UTF-8') + "\0"]
410
+ end
411
+
412
+ # Merges multiple PDF documents into one. Call close when done.
413
+ #
414
+ # Usage:
415
+ # merger = PdfMerger.new(fns)
416
+ # merger.add_bytes(File.binread('a.pdf'))
417
+ # merger.add_bytes(File.binread('b.pdf'))
418
+ # merger.save('/tmp/merged.pdf')
419
+ # merger.close
420
+ class PdfMerger
421
+ def initialize(fns)
422
+ @fns = fns
423
+ h = fns[:pdf_merge_create].call
424
+ raise "pdf_merge_create failed: #{PdfLibrary.last_error_str(fns)}" if h.nil? || h.zero?
425
+
426
+ @handle = h
427
+ end
428
+
429
+ # Add PDF bytes to the merge queue.
430
+ def add_bytes(data)
431
+ ptr = Fiddle::Pointer[data]
432
+ ret = @fns[:pdf_merge_add_bytes].call(@handle, ptr, data.bytesize)
433
+ raise "pdf_merge_add_bytes failed: #{PdfLibrary.last_error_str(@fns)}" unless ret.zero?
434
+
435
+ self
436
+ end
437
+
438
+ def save(path)
439
+ ret = @fns[:pdf_merge_save_file].call(@handle, c_str(path))
440
+ raise "pdf_merge_save_file failed: #{PdfLibrary.last_error_str(@fns)}" unless ret.zero?
441
+ end
442
+
443
+ # Merge and return PDF bytes.
444
+ def save_to_memory
445
+ tmp = Tempfile.new(['pdfnative', '.pdf'], binmode: true)
446
+ tmp_path = tmp.path
447
+ tmp.close
448
+ begin
449
+ save(tmp_path)
450
+ File.binread(tmp_path)
451
+ ensure
452
+ File.delete(tmp_path) if File.exist?(tmp_path)
453
+ end
454
+ end
455
+
456
+ def close
457
+ return unless @handle
458
+
459
+ @fns[:pdf_merge_close].call(@handle)
460
+ @handle = nil
461
+ end
462
+
463
+ private
464
+
465
+ def c_str(str) = Fiddle::Pointer[str.encode('UTF-8') + "\0"]
466
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MajorsilencePdf
4
+ VERSION = '26.0.1'
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'majorsilence_pdf/version'
4
+ require_relative 'majorsilence_pdf/pdf_native'
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: majorsilence_pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 26.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Gill
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: fiddle
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.1'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '1.1'
26
+ - !ruby/object:Gem::Dependency
27
+ name: minitest
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '5.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rake
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '13.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '13.0'
54
+ description: Ruby wrapper for the Majorsilence PDF native library -- create PDF documents
55
+ in-process via an AOT-compiled shared library (Fiddle FFI), no .NET runtime required.
56
+ Supports text styling, shapes, images, tables, fonts, encryption, annotations, hyperlinks,
57
+ and PDF merging.
58
+ email:
59
+ - peter@majorsilence.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - LICENSE
65
+ - LICENSE-APACHE
66
+ - LICENSE-BSD
67
+ - LICENSE-MIT
68
+ - README.md
69
+ - lib/majorsilence_pdf.rb
70
+ - lib/majorsilence_pdf/pdf_native.rb
71
+ - lib/majorsilence_pdf/version.rb
72
+ homepage: https://github.com/majorsilence/Reporting
73
+ licenses:
74
+ - MIT
75
+ - Apache-2.0
76
+ - BSD-2-Clause
77
+ metadata:
78
+ homepage_uri: https://github.com/majorsilence/Reporting
79
+ source_code_uri: https://github.com/majorsilence/pdf-ruby
80
+ changelog_uri: https://github.com/majorsilence/pdf-ruby/releases
81
+ bug_tracker_uri: https://github.com/majorsilence/pdf-ruby/issues
82
+ rubygems_mfa_required: 'true'
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '3.0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubygems_version: 4.0.10
98
+ specification_version: 4
99
+ summary: Ruby bindings for the Majorsilence PDF library
100
+ test_files: []