3 namespace Wallabag\CoreBundle\Helper
;
6 use PHPePub\Core\Structure\OPF\DublinCore
;
7 use Symfony\Component\HttpFoundation\Response
;
14 private $entries = array();
15 private $authors = array('wallabag');
16 private $language = '';
17 private $tags = array();
18 private $footerTemplate = '<div style="text-align:center;">
19 <p>Produced by wallabag with %EXPORT_METHOD%</p>
20 <p>Please open <a href="https://github.com/wallabag/wallabag/issues">an issue</a> if you have trouble with the display of this E-Book on your device.</p>
24 * @param string $wallabagUrl Wallabag instance url
25 * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
27 public function __construct($wallabagUrl, $logoPath)
29 $this->wallabagUrl
= $wallabagUrl;
30 $this->logoPath
= $logoPath;
36 * @param array|Entry $entries An array of entries or one entry
38 public function setEntries($entries)
40 if (!is_array($entries)) {
41 $this->language
= $entries->getLanguage();
42 $entries = array($entries);
45 $this->entries
= $entries;
47 foreach ($entries as $entry) {
48 $this->tags
[] = $entry->getTags();
55 * Sets the category of which we want to get articles, or just one entry.
57 * @param string $method Method to get articles
59 public function updateTitle($method)
61 $this->title
= $method.' articles';
63 if ('entry' === $method) {
64 $this->title
= $this->entries
[0]->getTitle();
71 * Sets the output format.
73 * @param string $format
75 public function exportAs($format)
79 return $this->produceEpub();
82 return $this->produceMobi();
85 return $this->producePDF();
88 return $this->produceCSV();
91 throw new \
InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
95 * Use PHPePub to dump a .epub file.
97 private function produceEpub()
100 * Start and End of the book
103 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
104 ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
106 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
107 ."<title>wallabag articles book</title>\n"
111 $bookEnd = "</body>\n</html>\n";
113 $book = new EPub(EPub
::BOOK_VERSION_EPUB3
);
119 $book->setTitle($this->title
);
120 // Could also be the ISBN number, prefered for published books, or a UUID.
121 $book->setIdentifier($this->title
, EPub
::IDENTIFIER_URI
);
122 // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc.
123 $book->setLanguage($this->language
);
124 $book->setDescription('Some articles saved on my wallabag');
126 foreach ($this->authors
as $author) {
127 $book->setAuthor($author, $author);
130 // I hope this is a non existant address :)
131 $book->setPublisher('wallabag', 'wallabag');
132 // Strictly not needed as the book date defaults to time().
133 $book->setDate(time());
134 $book->setSourceURL($this->wallabagUrl
);
136 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'PHP');
137 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'wallabag');
142 if (file_exists($this->logoPath
)) {
143 $book->setCoverImage('Cover.png', file_get_contents($this->logoPath
), 'image/png');
146 $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
151 * Adding actual entries
154 // set tags as subjects
155 foreach ($this->entries
as $entry) {
156 foreach ($this->tags
as $tag) {
157 $book->setSubject($tag['value']);
160 $chapter = $content_start.$entry->getContent().$bookEnd;
161 $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub
::EXTERNAL_REF_ADD
);
164 return Response
::create(
168 'Content-Description' => 'File Transfer',
169 'Content-type' => 'application/epub+zip',
170 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.epub"',
171 'Content-Transfer-Encoding' => 'binary',
177 * Use PHPMobi to dump a .mobi file.
179 private function produceMobi()
182 $content = new \
MOBIFile();
187 $content->set('title', $this->title
);
188 $content->set('author', implode($this->authors
));
189 $content->set('subject', $this->title
);
194 $content->appendParagraph($this->getExportInformation('PHPMobi'));
195 if (file_exists($this->logoPath
)) {
196 $content->appendImage(imagecreatefrompng($this->logoPath
));
198 $content->appendPageBreak();
201 * Adding actual entries
203 foreach ($this->entries
as $entry) {
204 $content->appendChapterTitle($entry->getTitle());
205 $content->appendParagraph($entry->getContent());
206 $content->appendPageBreak();
208 $mobi->setContentProvider($content);
210 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
211 $this->title
= preg_replace('/[^A-Za-z0-9\-]/', '', $this->title
);
213 return Response
::create(
217 'Accept-Ranges' => 'bytes',
218 'Content-Description' => 'File Transfer',
219 'Content-type' => 'application/x-mobipocket-ebook',
220 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.mobi"',
221 'Content-Transfer-Encoding' => 'binary',
227 * Use TCPDF to dump a .pdf file.
229 private function producePDF()
231 $pdf = new \
TCPDF(PDF_PAGE_ORIENTATION
, PDF_UNIT
, PDF_PAGE_FORMAT
, true, 'UTF-8', false);
236 $pdf->SetCreator(PDF_CREATOR
);
237 $pdf->SetAuthor('wallabag');
238 $pdf->SetTitle($this->title
);
239 $pdf->SetSubject('Articles via wallabag');
240 $pdf->SetKeywords('wallabag');
246 $intro = '<h1>'.$this->title
.'</h1>'.$this->getExportInformation('tcpdf');
248 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
251 * Adding actual entries
253 foreach ($this->entries
as $entry) {
254 foreach ($this->tags
as $tag) {
255 $pdf->SetKeywords($tag['value']);
259 $html = '<h1>'.$entry->getTitle().'</h1>';
260 $html .= $entry->getContent();
262 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
265 // set image scale factor
266 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO
);
268 return Response
::create(
269 $pdf->Output('', 'S'),
272 'Content-Description' => 'File Transfer',
273 'Content-type' => 'application/pdf',
274 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.pdf"',
275 'Content-Transfer-Encoding' => 'binary',
281 * Inspired from CsvFileDumper.
283 private function produceCSV()
287 $handle = fopen('php://memory', 'rb+');
289 fputcsv($handle, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'), $delimiter, $enclosure);
291 foreach ($this->entries
as $entry) {
297 $entry->getContent(),
298 implode(', ', $entry->getTags()->toArray()),
299 $entry->getMimetype(),
300 $entry->getLanguage(),
308 $output = stream_get_contents($handle);
311 return Response
::create(
315 'Content-type' => 'application/csv',
316 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.csv"',
317 'Content-Transfer-Encoding' => 'UTF-8',
323 * Return a kind of footer / information for the epub.
325 * @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi
329 private function getExportInformation($type)
331 $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate
);
333 if ('tcpdf' === $type) {
334 return str_replace('%IMAGE%', '<img src="'.$this->logoPath
.'" />', $info);
337 return str_replace('%IMAGE%', '', $info);