ePub는 Google Play 북과 같이 Amazon Kindle 이외의 대부분의 eBook 리더에서 지원하는 eBook의 일반적인 형식입니다. epub-gen npm 모듈을 사용하면 HTML 문서에서 ePub를 만들 수 있습니다. 작동 방식은 다음과 같습니다.
시작하기
.epub 파일은 eBook에 대한 HTML, 이미지 및 메타 데이터를 포함하는 압축 파일입니다. 불행히도 ePub 형식은 간결하며 모든 독자에게 ePub가 올바르게 작동하는지 테스트하기가 어렵기 때문에 사전 구축 된 도구를 사용하는 것이 좋습니다.
먼저 epub-gen을 설치하십시오 :
npm install epub-gen
epub-gen에는 제목, 작성자, epub를 작성하는 출력 경로 및 컨텐츠 배열의 4 가지 필수 매개 변수가 있습니다. 내용의 각 항목은 책의 한 장에 해당합니다. 예를 들어, Moby-Dick의 처음 몇 단어가 포함 된 epub을 생성하는 방법은 다음과 같습니다.
const epub = require('epub-gen');
const options = {
title: 'Moby-Dick',
author: 'Herman Melville',
output: './moby-dick.epub',
content: [
{
title: 'Chapter 1: Loomings',
data: `<p>
Call me Ishmael. Some years ago—never mind how long precisely
</p>`
}
]
};
new epub(options).promise.then(() => console.log('Done'));
EPUBReader Chrome 확장 프로그램에서 위의 PDF 모양은 다음과 같습니다.
약간의 추가 작업으로 Axios를 사용하여 Project Gutenberg에서 전체 텍스트 버전의 Moby-Dick을 다운로드 할 수 있습니다.
const axios = require('axios');
const epub = require('epub-gen');
axios.get('http://www.gutenberg.org/files/2701/2701-0.txt').
then(res => res.data).
then(text => {
text = text.slice(text.indexOf('EXTRACTS.'));
text = text.slice(text.indexOf('CHAPTER 1.'));
const lines = text.split('\r\n');
const content = [];
for (let i = 0; i < lines.length; ++i) {
const line = lines[i];
if (line.startsWith('CHAPTER ')) {
if (content.length) {
content[content.length - 1].data = content[content.length - 1].data.join('\n');
}
content.push({
title: line,
data: ['<h2>' + line + '</h2>']
});
} else if (line.trim() === '') {
if (content[content.length - 1].data.length > 1) {
content[content.length - 1].data.push('</p>');
}
content[content.length - 1].data.push('<p>');
} else {
content[content.length - 1].data.push(line);
}
}
const options = {
title: 'Moby-Dick',
author: 'Herman Melville',
output: './moby-dick.epub',
content
};
return new epub(options).promise;
}).
then(() => console.log('Done'));
스타일링 및 서식
epub-gen에서는 사용자 정의 CSS 및 글꼴을 지정할 수 있습니다. 예를 들어, 선 높이를 1.5로 설정하고 Google 글꼴에서 맞춤 글꼴을 사용하는 방법은 다음과 같습니다.
const options = {
title: 'Moby-Dick',
author: 'Herman Melville',
output: './moby-dick.epub',
css: `
* { font-family: 'PT Serif'; }
p { line-height: 1.5em; }
`,
// You need to download the TTF file from Google Fonts
fonts: ['./PT_Serif/PTSerif-Regular.ttf'],
content
};
아래는 새로 형식이 지정된 eBook의 모습입니다.
사용자 정의 표지를 추가 할 수도 있습니다. 표지는 이미지 여야 하며 HTML 또는 PDF를 사용할 수 없습니다.
const options = {
title: 'Moby-Dick',
author: 'Herman Melville',
output: './moby-dick.epub',
cover: './moby-dick.jpg',
css: `
* { font-family: 'PT Serif'; }
p { line-height: 1.5em; }
`,
// You need to download the TTF file from Google Fonts
fonts: ['./PT_Serif/PTSerif-Regular.ttf'],
content
};
다음은 멋진 새 표지가 포함 된 Google Play 북에서 epub의 모습입니다.
계속
ePub는 PDF, Mobi 및 Kindle의 독점 형식과 함께 eBook에서 가장 널리 사용되는 형식 중 하나입니다. PDF보다 더 구조화되어 있고 바닐라 CSS를 사용하여 스타일을 쉽게 지정할 수 있다는 장점이 있습니다. 다음에 여름 독서를 위해 멋진 형식의 책을 원한다면 ePub를 생성 해보십시오.
등록된 댓글이 없습니다.