View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.cms.core.html;
17  
18  import java.io.ByteArrayOutputStream;
19  import java.io.IOException;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.seasar.tuigwaa.cms.core.CmsConstants;
26  import org.seasar.tuigwaa.cms.core.CmsRequest;
27  import org.seasar.tuigwaa.cms.core.CmsResponse;
28  import org.seasar.tuigwaa.cms.core.Resource;
29  import org.seasar.tuigwaa.cms.core.pdf.PdfCmsPageEvents;
30  import org.seasar.tuigwaa.cms.core.pdf.PdfUtils;
31  import org.seasar.tuigwaa.cms.core.wiki.base.WikiConfiguration;
32  
33  import com.lowagie.text.Document;
34  import com.lowagie.text.DocumentException;
35  import com.lowagie.text.Element;
36  import com.lowagie.text.Rectangle;
37  import com.lowagie.text.pdf.PdfPageEvent;
38  import com.lowagie.text.pdf.PdfWriter;
39  
40  /***
41   * @author someda
42   */
43  public class PdfConverter {
44  
45  	private CmsRequest request;
46  
47  	private CmsResponse response;
48  
49  	private WikiConfiguration config;
50  
51  	private static final float PAGEMARGIN_TOP = 50;
52  
53  	private static final float PAGEMARGIN_RIGHT = 50;
54  
55  	private static final float PAGEMARGIN_BOTTOM = 50;
56  
57  	private static final float PAGEMARGIN_LEFT = 50;
58  
59  	private Log log = LogFactory.getLog(getClass());
60  
61  	public PdfConverter(CmsRequest request, CmsResponse response,
62  			WikiConfiguration config) {
63  		this.request = request;
64  		this.response = response;
65  		this.config = config;
66  	}
67  
68  	public byte[] convertHtmlToPdf(String htmlContents) {
69  
70  		Object pdfContents = PdfUtils.parseHTMLtoPDF(htmlContents);
71  		byte[] buf = null;
72  		if (pdfContents != null) {
73  			try {
74  				Resource resource = request.getPage().getResource();
75  				String style = resource.getProperty(CmsConstants.PROPERTY_PDFSTYLE);
76  				if(style == null) style = PdfUtils.DEFAULT_PDF_PAGESTYLE;
77  								
78  				Rectangle rec = PdfUtils.getRectangle(style);				
79  				Document doc = new Document(rec, PAGEMARGIN_LEFT,PAGEMARGIN_RIGHT, PAGEMARGIN_TOP, PAGEMARGIN_BOTTOM);
80  				ByteArrayOutputStream baos = new ByteArrayOutputStream();
81  				PdfWriter writer = PdfWriter.getInstance(doc, baos);
82  				
83  				String pagePath = resource.getPath();
84  				PdfPageEvent event = new PdfCmsPageEvents(pagePath,PdfUtils.FONT_JA_GOTHIC,style);
85  				writer.setPageEvent(event);
86  
87  				doc.addAuthor(resource.getModificationUser());
88  				doc.addCreationDate();
89  				doc.addSubject(request.getPage().getResource().getPath());
90  				// doc.addCreator(CREATER_APPLICATION);
91  				doc.open();
92  
93  				if (pdfContents instanceof Element) {
94  					doc.add((Element) pdfContents);
95  				} else if (pdfContents instanceof List) {
96  					List el = (List) pdfContents;
97  					for (Iterator i = el.iterator(); i.hasNext();) {
98  						Element e = (Element) i.next();
99  						if (e != null)
100 							doc.add(e);
101 					}
102 				} else {
103 					log.info("ignore unknown object type :"
104 							+ pdfContents.getClass());
105 				}
106 				doc.close();
107 
108 				try {
109 					buf = baos.toByteArray();
110 					baos.close();
111 				} catch (IOException ioe) {
112 					ioe.printStackTrace();
113 					int nerror = response.getNParseError();
114 					response.setNParseError(++nerror);
115 				}
116 
117 			} catch (DocumentException de) {
118 				de.printStackTrace();
119 			}
120 		}
121 		return buf;
122 	}
123 
124 }