]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document/src/org/simantics/document/Exportable.java
Filter also history items in resource search dialog
[simantics/platform.git] / bundles / org.simantics.document / src / org / simantics / document / Exportable.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.document;
13
14 import java.io.BufferedInputStream;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.net.URL;
20
21 import org.eclipse.mylyn.wikitext.mediawiki.MediaWikiLanguage;
22 import org.eclipse.mylyn.wikitext.parser.MarkupParser;
23 import org.simantics.Simantics;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.utils.Logger;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.utils.FileUtils;
29 import org.simantics.utils.ui.BundleUtils;
30 import org.simantics.wiki.ui.editor.IExportable;
31
32 import com.lowagie.text.Document;
33 import com.lowagie.text.DocumentException;
34 import com.lowagie.text.PageSize;
35 import com.lowagie.text.Utilities;
36 import com.lowagie.text.pdf.PdfContentByte;
37 import com.lowagie.text.pdf.PdfImportedPage;
38 import com.lowagie.text.pdf.PdfReader;
39 import com.lowagie.text.pdf.PdfWriter;
40
41 public class Exportable implements IExportable {
42
43         final private String html;
44         final private DocumentSettings settings;
45
46         private static final String DEFAULT_CSS;
47         public static final String DEFAULT_MODEL_CSS;
48
49         private static String getBundleFileContents(String path, String defaultValue) {
50                 URL url = BundleUtils.find(Activator.getContext().getBundle(), path);
51                 if (url == null)
52                         return defaultValue;
53                 try (InputStream in = url.openStream()) {
54                         return FileUtils.getContents(in);
55                 } catch (IOException e) {
56                         Logger.defaultLogError(e);
57                         return defaultValue;
58                 }
59         }
60
61         static {
62                 DEFAULT_CSS = getBundleFileContents("simantics-wiki-documents.css", "");
63                 DEFAULT_MODEL_CSS = getBundleFileContents("simantics-wiki-documents-default-model.css", "");
64         }
65         
66         public Exportable(ReadGraph graph, Resource res, String wiki, String css, DocumentSettings settings, boolean print) {
67
68                 try {
69                         wiki = DocumentDialect.INSTANCE.apply(graph, res, wiki);
70                 } catch (DatabaseException e) {
71                         Logger.defaultLogError(e);
72                 }
73                 
74                 this.settings = settings;
75                 
76                 MarkupParser markupParser = new MarkupParser();
77                 markupParser.setMarkupLanguage(new MediaWikiLanguage());
78                 String html = markupParser.parseToHtml(wiki);
79                 
80                 String width = "width:" + (210-settings.marginLeft-settings.marginRight) + "mm;";
81
82                 if(print) {
83                         html = html.replace("<body>", "<body style=\"background-color:#fff\"><div style=\"background-color:#FFF;" + width + "\">");
84                         html = html.replace("</body>", "</div></body>");
85                 } else {
86                         String div1 = "margin-left:3mm;margin-top:3mm;background-color:#FFF;width:210mm;"; 
87                         String div2 = "background-color:#FFF;width:210mm;padding-top:" + settings.marginTop + "mm;";
88                         String div3 = "overflow-x:hidden;margin-left:" + settings.marginLeft + "mm;background-color:#FFF;" + width; 
89                         html = html.replace("<body>", "<body><div style=\"" + div1 + "\"><div style=\"" + div2 + "\"><div style=\"" + div3 + "\">");
90                         html = html.replace("</body>", "</div></div></div></body>");
91                 }
92                 
93                 html = html.replace("<td>", "<td><div>");
94                 html = html.replace("<th>", "<th><div>");
95                 html = html.replace("</td>", "</div></td>");
96                 html = html.replace("</th>", "</div></th>");
97                 html = html.replace("</head>", "<style type=\"text/css\">\n" + DEFAULT_CSS + css +  "\n</style>\n</head>\n");
98                 
99                 this.html = html;
100                 
101         }
102         
103         public String getHTML() {
104                 return html;
105         }
106
107         @Override
108         public int export(Document document, PdfWriter writer) throws DocumentException {
109
110                 File temp = Simantics.getTempfile("wikiPdfExport", "pdf");
111                 try {
112                         
113                         temp.getParentFile().mkdirs();
114                         PhantomJSDriver.print(html, settings, temp);
115                         
116                         int result = 0;
117                 PdfContentByte cb = writer.getDirectContent();
118                 PdfReader reader = new PdfReader(new BufferedInputStream(new FileInputStream(temp)));
119                 for (int i = 1; i <= reader.getNumberOfPages(); i++) {
120                         document.setPageSize(PageSize.A4);
121                         document.newPage();
122                         //import the page from source pdf
123                         PdfImportedPage page = writer.getImportedPage(reader, i);
124                         //add the page to the destination pdf
125                         float pts = Utilities.millimetersToPoints(10);
126                         cb.addTemplate(page, pts, pts);
127                         ++result;
128                 }
129
130                 return result;
131                 } catch (IOException e) {
132
133                         throw new DocumentException(e);
134                         
135                 }
136
137         }
138
139 }