001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.pdftools; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032import org.opencms.jsp.util.CmsJspStandardContextBean; 033import org.opencms.loader.CmsTemplateLoaderFacade; 034import org.opencms.main.OpenCms; 035import org.opencms.xml.containerpage.CmsContainerElementBean; 036 037import java.util.Collections; 038 039import jakarta.servlet.http.HttpServletRequest; 040import jakarta.servlet.http.HttpServletResponse; 041 042/** 043 * Utility class for PDF formatting.<p> 044 */ 045public final class CmsPdfFormatterUtils { 046 047 /** 048 * Private constructor to prevent instantiation.<p> 049 */ 050 private CmsPdfFormatterUtils() { 051 052 // do nothing 053 } 054 055 /** 056 * Executes a JSP with a given content as input and returns the output of the JSP.<p> 057 * 058 * @param cms the current CMS context 059 * @param request the current request 060 * @param response the current response 061 * @param jsp the jsp resource to execute 062 * @param content the content to render with the JSP 063 * 064 * @return the output of the JSP 065 * @throws Exception if something goes wrong 066 */ 067 public static byte[] executeJsp( 068 CmsObject cms, 069 HttpServletRequest request, 070 HttpServletResponse response, 071 CmsResource jsp, 072 CmsResource content) 073 throws Exception { 074 075 CmsTemplateLoaderFacade loaderFacade = new CmsTemplateLoaderFacade( 076 OpenCms.getResourceManager().getLoader(jsp), 077 content, 078 jsp); 079 CmsResource loaderRes = loaderFacade.getLoaderStartResource(); 080 request.setAttribute(CmsJspStandardContextBean.ATTRIBUTE_CMS_OBJECT, cms); 081 CmsJspStandardContextBean context = CmsJspStandardContextBean.getInstance(request); 082 CmsContainerElementBean element = new CmsContainerElementBean( 083 content.getStructureId(), 084 jsp.getStructureId(), 085 Collections.<String, String> emptyMap(), 086 false); 087 context.setElement(element); 088 return loaderFacade.getLoader().dump( 089 cms, 090 loaderRes, 091 null, 092 cms.getRequestContext().getLocale(), 093 request, 094 response); 095 } 096 097}