blob: 5220dbeb071193a128a4dab722912447c5f53ae6 [file] [log] [blame]
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org.ofbiz.webapp.view;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.GeneralException;
/**
* Uses XSL-FO formatted templates to generate PDF views
* This handler will use JPublish to generate the XSL-FO
*/
public class FopPdfViewHandler extends JPublishViewHandler {
public static final String module = FopPdfViewHandler.class.getName();
/**
* @see org.ofbiz.webapp.view.ViewHandler#render(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void render(String name, String page, String info, String contentType, String encoding, HttpServletRequest request, HttpServletResponse response) throws ViewHandlerException {
// render and obtain the XSL-FO
Writer writer = new StringWriter();
try {
wrapper.render(page, request, response, writer, null, false);
} catch (Throwable t) {
throw new ViewHandlerException("Problems with the response writer/output stream", t);
}
if (Debug.verboseOn()) {
Debug.logVerbose("XSL-FO : " + writer.toString(), module);
}
// render the byte array
ByteArrayOutputStream out = null;
Reader reader = new StringReader(writer.toString());
Source src = new StreamSource(reader);
try {
ApacheFopWorker.transform(src, out, null, contentType);
} catch (GeneralException e) {
throw new ViewHandlerException(e.getMessage(), e.getNested());
}
// set the content type and length
response.setContentType(contentType);
response.setContentLength(out.size());
// write to the browser
try {
out.writeTo(response.getOutputStream());
response.getOutputStream().flush();
} catch (IOException e) {
throw new ViewHandlerException("Unable write to browser OutputStream", e);
}
}
}