hwayo 0.1.0 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6b649626b8aa64f7e09efcca03cbc6aaf09564fa7fedd669603f58df2cd2255e
4
- data.tar.gz: d3390414f88e33b5c25d0219d9cc41a9b12674b3d52f180a225bd9eb4768364e
3
+ metadata.gz: 56b0f33f4688b679d29dd4624f08eb5878756a7b63d6ce2ddaaa3f5a6e3ede68
4
+ data.tar.gz: 25301f388cc45b11e106b6b9d232949b9b500020cc060c58efb380203bb6b18e
5
5
  SHA512:
6
- metadata.gz: 83f850bf7491f9c6d0caf3b0ede1720d82b6c9c7e8644b0b2d4735622b343e07036f980eaefd115bda21a374ac8d93533efb1773c38e042226ee2b7b8a307d4b
7
- data.tar.gz: 20916130d516ddd212721212ae518851b92efba543868bf49b0006c76d4ebf531b9f23493be6f262d98b74d0c06ef54d6d81b3bcf7054f9e9465329428b0fe9b
6
+ metadata.gz: b9f5179e4859fe79df367eb296ec721b4aa7ec37cb4ee0f165b1b44fd6cc6c0ac6538086f03e86ca4e8c1a8116461e269f2ca8e4606ba303d41c8b7ad23acf21
7
+ data.tar.gz: ce6ed80928e916998043dc0b213107fd36c07e0ead01338353a9e2ea1325bddd5c6af890c81a947ed3882d493d825f8e507825a5802cbc4609c3e62ef840a582
data/README.md CHANGED
@@ -1,10 +1,27 @@
1
1
  # Hwayo
2
2
 
3
- Hwayo는 한글(HWP) 파일에서 텍스트를 추출하는 Ruby gem입니다. Java 기반의 hwplib 라이브러리를 Ruby에서 사용할 수 있도록 래핑하였습니다.
3
+ [![Gem Version](https://badge.fury.io/rb/hwayo.svg)](https://badge.fury.io/rb/hwayo)
4
+
5
+ Hwayo는 한글(HWP) 및 PDF 파일에서 텍스트를 추출하는 Ruby gem입니다. Java 기반의 [hwplib](https://github.com/neolord0/hwplib)와 [Apache PDFBox](https://pdfbox.apache.org/) 라이브러리를 Ruby에서 쉽게 사용할 수 있도록 래핑하였습니다.
6
+
7
+ ## 특징
8
+
9
+ - 한글(HWP) 파일에서 텍스트 추출
10
+ - PDF 파일에서 텍스트 추출 (Apache PDFBox 3.0.5 사용)
11
+ - 표, 그림 설명 등 컨트롤 텍스트 포함 추출
12
+ - 간단한 Ruby 인터페이스
13
+ - 파일 저장 또는 문자열 반환 선택 가능
14
+
15
+ ## 요구사항
16
+
17
+ - Ruby 2.7.0 이상
18
+ - Java 8 이상 설치 필요
19
+ - hwplib JAR 파일 (자동 포함 또는 별도 다운로드)
20
+ - PDFBox JAR 파일 (자동 포함)
4
21
 
5
22
  ## 설치
6
23
 
7
- Gemfile에 다음을 추가하세요:
24
+ Gemfile에 추가:
8
25
 
9
26
  ```ruby
10
27
  gem 'hwayo'
@@ -28,6 +45,15 @@ require 'hwayo'
28
45
  # HWP 파일에서 텍스트 추출
29
46
  result = Hwayo.extract_text('document.hwp')
30
47
 
48
+ if result[:success]
49
+ puts result[:text]
50
+ else
51
+ puts "Error: #{result[:error]}"
52
+ end
53
+
54
+ # PDF 파일에서 텍스트 추출
55
+ result = Hwayo.extract_text('document.pdf')
56
+
31
57
  if result[:success]
32
58
  puts result[:text]
33
59
  else
@@ -38,37 +64,177 @@ end
38
64
  ### 파일로 저장
39
65
 
40
66
  ```ruby
41
- # 추출한 텍스트를 파일로 저장
67
+ # HWP에서 추출한 텍스트를 파일로 저장
42
68
  result = Hwayo.extract_text('document.hwp', 'output.txt')
43
69
 
44
70
  if result[:success]
45
- puts "Text saved to: #{result[:output_path]}"
71
+ puts "텍스트가 저장되었습니다: #{result[:output_path]}"
46
72
  else
47
- puts "Error: #{result[:error]}"
73
+ puts "오류 발생: #{result[:error]}"
74
+ end
75
+
76
+ # PDF에서 추출한 텍스트를 파일로 저장
77
+ result = Hwayo.extract_text('document.pdf', 'output.txt')
78
+
79
+ if result[:success]
80
+ puts "텍스트가 저장되었습니다: #{result[:output_path]}"
81
+ else
82
+ puts "오류 발생: #{result[:error]}"
48
83
  end
49
84
  ```
50
85
 
51
- ## 요구사항
86
+ ### 여러 파일 일괄 처리
52
87
 
53
- - Ruby 2.7.0 이상
54
- - Java 8 이상 (JAVA_HOME 환경변수 설정 필요)
55
- - MRI Ruby의 경우 RJB gem 필요 (자동 설치됨)
56
- - JRuby의 경우 추가 설정 없이 사용 가능
88
+ ```ruby
89
+ # HWP와 PDF 파일 모두 처리
90
+ files = Dir.glob("*.{hwp,pdf}")
91
+
92
+ files.each do |file|
93
+ output_file = file.gsub(/\.(hwp|pdf)$/i, '.txt')
94
+ result = Hwayo.extract_text(file, output_file)
95
+
96
+ if result[:success]
97
+ puts "✓ #{file} → #{output_file}"
98
+ else
99
+ puts "✗ #{file}: #{result[:error]}"
100
+ end
101
+ end
102
+ ```
103
+
104
+ ### Rails에서 사용하기
105
+
106
+ ```ruby
107
+ class DocumentsController < ApplicationController
108
+ def extract
109
+ uploaded_file = params[:file]
110
+
111
+ if uploaded_file.present?
112
+ # 임시 파일로 저장
113
+ temp_file = Tempfile.new(['document', '.hwp'])
114
+ temp_file.binmode
115
+ temp_file.write(uploaded_file.read)
116
+ temp_file.rewind
117
+
118
+ # 텍스트 추출
119
+ result = Hwayo.extract_text(temp_file.path)
120
+
121
+ # 정리
122
+ temp_file.close
123
+ temp_file.unlink
124
+
125
+ if result[:success]
126
+ render json: { text: result[:text] }
127
+ else
128
+ render json: { error: result[:error] }, status: :unprocessable_entity
129
+ end
130
+ else
131
+ render json: { error: 'No file provided' }, status: :bad_request
132
+ end
133
+ end
134
+ end
135
+ ```
136
+
137
+ ## 설정
138
+
139
+ ### JAR 파일 위치 지정
140
+
141
+ hwayo는 다음 순서로 JAR 파일을 찾습니다:
142
+
143
+ **HWP 처리용 hwplib JAR:**
144
+ 1. gem 내부의 `lib/hwayo/java/hwplib-1.1.10.jar`
145
+ 2. 현재 디렉토리의 `hwplib-1.1.10.jar`
146
+ 3. 현재 디렉토리의 `target/hwplib-1.1.10.jar`
147
+ 4. 환경 변수 `HWPLIB_JAR_PATH`
57
148
 
58
- ## 개발
149
+ **PDF 처리용 PDFBox JAR:**
150
+ 1. gem 내부의 `lib/hwayo/java/pdfbox-app-3.0.5.jar`
151
+ 2. 현재 디렉토리의 `pdfbox-app-3.0.5.jar`
152
+ 3. 현재 디렉토리의 `target/pdfbox-app-3.0.5.jar`
153
+ 4. 환경 변수 `PDFBOX_JAR_PATH`
59
154
 
60
- gem을 개발하려면:
155
+ 커스텀 경로 지정:
61
156
 
62
- 1. 저장소를 클론합니다
63
- 2. `bin/setup`을 실행하여 의존성을 설치합니다
64
- 3. `rake spec`을 실행하여 테스트를 실행합니다
65
- 4. `bin/console`을 실행하여 대화형 프롬프트를 시작합니다
157
+ ```bash
158
+ export HWPLIB_JAR_PATH=/path/to/hwplib-1.1.10.jar
159
+ export PDFBOX_JAR_PATH=/path/to/pdfbox-app-3.0.5.jar
160
+ ```
161
+
162
+ ### Docker에서 사용하기
163
+
164
+ Dockerfile에 Java 설치:
165
+
166
+ ```dockerfile
167
+ # Debian/Ubuntu
168
+ RUN apt-get update && apt-get install -y openjdk-11-jre-headless
169
+
170
+ # Alpine Linux
171
+ RUN apk add --no-cache openjdk11-jre
172
+
173
+ # 환경 변수 설정
174
+ ENV HWPLIB_JAR_PATH=/app/vendor/hwplib-1.1.10.jar
175
+ ```
176
+
177
+ ## 문제 해결
178
+
179
+ ### Java가 설치되지 않음
180
+
181
+ ```
182
+ Error: Java is not installed. Please install Java 8 or later.
183
+ ```
184
+
185
+ 해결 방법:
186
+
187
+ ```bash
188
+ # macOS
189
+ brew install openjdk@11
190
+
191
+ # Ubuntu/Debian
192
+ sudo apt-get install openjdk-11-jre
193
+
194
+ # CentOS/RHEL
195
+ sudo yum install java-11-openjdk
196
+ ```
66
197
 
67
- ## 기여
198
+ ### JAR 파일을 찾을 수 없음
68
199
 
69
- 버그 리포트와 풀 리퀘스트는 GitHub에서 환영합니다.
200
+ ```
201
+ Error: hwplib JAR not found
202
+ ```
203
+
204
+ 해결 방법:
205
+
206
+ 1. [hwplib releases](https://github.com/neolord0/hwplib/releases)에서 JAR 파일 다운로드
207
+ 2. 환경 변수 설정: `export HWPLIB_JAR_PATH=/path/to/hwplib-1.1.10.jar`
208
+
209
+ ### 한글 깨짐
210
+
211
+ UTF-8 인코딩을 확인하세요:
212
+
213
+ ```ruby
214
+ result = Hwayo.extract_text('document.hwp')
215
+ puts result[:text].encoding # UTF-8이어야 함
216
+ ```
217
+
218
+ ## 제한사항
219
+
220
+ - 암호화된 HWP/PDF 파일은 지원하지 않습니다
221
+ - 이미지, HTML로 변환은 지원하지 않습니다
222
+ - Java 프로세스 실행으로 인한 오버헤드가 있습니다
223
+
224
+ ## 기여하기
225
+
226
+ 1. Fork it ( https://github.com/onesup/hwayo/fork )
227
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
228
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
229
+ 4. Push to the branch (`git push origin my-new-feature`)
230
+ 5. Create a new Pull Request
70
231
 
71
232
  ## 라이선스
72
233
 
73
234
  이 gem은 [MIT License](https://opensource.org/licenses/MIT)로 제공됩니다.
74
- hwplib은 [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0)으로 제공됩니다.
235
+
236
+ hwplib은 [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0)으로 제공됩니다.
237
+
238
+ ## 감사의 말
239
+
240
+ 이 gem은 [hwplib](https://github.com/neolord0/hwplib) 프로젝트를 기반으로 만들어졌습니다. hwplib 개발자분들께 감사드립니다.
Binary file
@@ -0,0 +1,103 @@
1
+ require 'open3'
2
+ require 'tempfile'
3
+
4
+ module Hwayo
5
+ module PdfExtractor
6
+ class << self
7
+ def extract_text(pdf_file, output_file = nil)
8
+ validate_inputs(pdf_file)
9
+ validate_java_installation
10
+
11
+ jar_path = find_pdfbox_jar
12
+
13
+ result = if output_file
14
+ extract_to_file(pdf_file, output_file, jar_path)
15
+ else
16
+ extract_to_string(pdf_file, jar_path)
17
+ end
18
+
19
+ format_result(result, output_file)
20
+ end
21
+
22
+ private
23
+
24
+ def validate_inputs(pdf_file)
25
+ raise ArgumentError, "PDF file path is required" if pdf_file.nil? || pdf_file.empty?
26
+ raise ArgumentError, "PDF file not found: #{pdf_file}" unless File.exist?(pdf_file)
27
+ raise ArgumentError, "Not a PDF file: #{pdf_file}" unless pdf_file.downcase.end_with?('.pdf')
28
+ end
29
+
30
+ def validate_java_installation
31
+ stdout, stderr, status = Open3.capture3('java', '-version')
32
+ unless status.success?
33
+ raise RuntimeError, "Java is not installed or not in PATH. Please install Java 8 or higher."
34
+ end
35
+ end
36
+
37
+ def find_pdfbox_jar
38
+ search_paths = [
39
+ File.join(File.dirname(__FILE__), 'java', 'pdfbox-app-3.0.5.jar'),
40
+ 'pdfbox-app-3.0.5.jar',
41
+ 'target/pdfbox-app-3.0.5.jar',
42
+ ENV['PDFBOX_JAR_PATH']
43
+ ].compact
44
+
45
+ jar_path = search_paths.find { |path| path && File.exist?(path) }
46
+
47
+ unless jar_path
48
+ raise RuntimeError, "PDFBox JAR file not found. Searched in: #{search_paths.join(', ')}"
49
+ end
50
+
51
+ jar_path
52
+ end
53
+
54
+ def extract_to_file(pdf_file, output_file, jar_path)
55
+ cmd = ['java', '-jar', jar_path, 'export:text', '-i', pdf_file, '-o', output_file]
56
+
57
+ stdout, stderr, status = Open3.capture3(*cmd)
58
+
59
+ {
60
+ success: status.success?,
61
+ stdout: stdout,
62
+ stderr: stderr,
63
+ output_path: output_file
64
+ }
65
+ end
66
+
67
+ def extract_to_string(pdf_file, jar_path)
68
+ Tempfile.create(['pdfbox_output', '.txt']) do |temp_file|
69
+ result = extract_to_file(pdf_file, temp_file.path, jar_path)
70
+
71
+ if result[:success]
72
+ result[:text] = File.read(temp_file.path, encoding: 'UTF-8')
73
+ end
74
+
75
+ result
76
+ end
77
+ end
78
+
79
+ def format_result(result, output_file)
80
+ if result[:success]
81
+ base_result = {
82
+ success: true,
83
+ message: "Successfully extracted text from PDF"
84
+ }
85
+
86
+ if output_file
87
+ base_result[:output_path] = result[:output_path]
88
+ else
89
+ base_result[:text] = result[:text]
90
+ end
91
+
92
+ base_result
93
+ else
94
+ {
95
+ success: false,
96
+ message: "Failed to extract text from PDF",
97
+ error: result[:stderr]
98
+ }
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
data/lib/hwayo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hwayo
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/hwayo.rb CHANGED
@@ -1,14 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "hwayo/version"
4
+ require_relative "hwayo/pdf_extractor"
4
5
  require 'open3'
5
6
  require 'tempfile'
6
7
 
7
8
  module Hwayo
8
9
  class Error < StandardError; end
9
10
 
11
+ # Extract text from HWP or PDF file
12
+ def self.extract_text(file_path, output_path = nil)
13
+ return extract_pdf(file_path, output_path) if file_path.downcase.end_with?('.pdf')
14
+ extract_hwp(file_path, output_path)
15
+ end
16
+
17
+ # Extract text from PDF file
18
+ def self.extract_pdf(pdf_file_path, output_path = nil)
19
+ PdfExtractor.extract_text(pdf_file_path, output_path)
20
+ end
21
+
10
22
  # Extract text from HWP file
11
- def self.extract_text(hwp_file_path, output_path = nil)
23
+ def self.extract_hwp(hwp_file_path, output_path = nil)
12
24
  raise Error, "HWP file not found: #{hwp_file_path}" unless File.exist?(hwp_file_path)
13
25
 
14
26
  # Java 실행 가능 여부 확인
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hwayo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 이원섭wonsup Lee/Alfonso
@@ -24,6 +24,8 @@ files:
24
24
  - lib/hwayo/extractor.rb
25
25
  - lib/hwayo/java/HWPTextExtractorCLI.class
26
26
  - lib/hwayo/java/hwplib-1.1.10.jar
27
+ - lib/hwayo/java/pdfbox-app-3.0.5.jar
28
+ - lib/hwayo/pdf_extractor.rb
27
29
  - lib/hwayo/simple_extractor.rb
28
30
  - lib/hwayo/version.rb
29
31
  - sig/hwayo.rbs