예제

HWPJS를 사용하는 다양한 예제를 확인하세요.

기본 예제

Web
React Native
Node.js
import * as hwpjs from '@ohah/hwpjs';

// 파일 입력에서 HWP 파일 읽기
const fileInput = document.querySelector('input[type="file"]');
fileInput.addEventListener('change', async (e) => {
  const file = (e.target as HTMLInputElement).files?.[0];
  if (!file) return;

  try {
    const arrayBuffer = await file.arrayBuffer();
    const data = new Uint8Array(arrayBuffer);

    // JSON으로 변환
    const result = hwpjs.toJson(data);
    console.log('변환 성공:', result);
  } catch (error) {
    console.error('변환 실패:', error);
  }
});

마크다운 변환

Web
React Native
Node.js
import * as hwpjs from '@ohah/hwpjs';

const fileInput = document.querySelector('input[type="file"]');
fileInput.addEventListener('change', async (e) => {
  const file = (e.target as HTMLInputElement).files?.[0];
  if (!file) return;

  try {
    const arrayBuffer = await file.arrayBuffer();
    const data = new Uint8Array(arrayBuffer);

    // 마크다운으로 변환
    const markdownResult = hwpjs.toMarkdown(data, {
      image: 'base64',
      useHtml: true,
      includeVersion: true,
      includePageInfo: false,
    });
    console.log('마크다운 변환 성공:', markdownResult.markdown);
  } catch (error) {
    console.error('변환 실패:', error);
  }
});

HTML 변환

Web
React Native
Node.js
import * as hwpjs from '@ohah/hwpjs';

const fileInput = document.querySelector('input[type="file"]');
fileInput.addEventListener('change', async (e) => {
  const file = (e.target as HTMLInputElement).files?.[0];
  if (!file) return;

  try {
    const arrayBuffer = await file.arrayBuffer();
    const data = new Uint8Array(arrayBuffer);

    // HTML로 변환 (이미지는 base64로 임베드)
    const html = hwpjs.toHtml(data, {
      includeVersion: true,
      includePageInfo: false,
    });
    
    // HTML을 화면에 표시
    const outputDiv = document.getElementById('output');
    if (outputDiv) {
      outputDiv.innerHTML = html;
    }
  } catch (error) {
    console.error('변환 실패:', error);
  }
});

HTML 변환 특징:

  • 기본적으로 이미지는 base64 데이터 URI로 HTML에 임베드됩니다
  • imageOutputDir 옵션을 지정하면 이미지를 별도 파일로 저장하고 상대 경로로 참조합니다
  • 완전한 HTML 문서 (<html>, <head>, <body> 포함)를 생성합니다
  • CSS 클래스 기반 스타일링을 사용합니다