📄 compdocservices.java
字号:
if (UtilValidate.isNotEmpty(surveyResponseId)) { surveyResponse = delegator.findByPrimaryKey("SurveyResponse", UtilMisc.toMap("surveyResponseId", surveyResponseId)); if (surveyResponse != null) { surveyId = surveyResponse.getString("surveyId"); } } if (UtilValidate.isNotEmpty(surveyId)) { GenericValue survey = delegator.findByPrimaryKey("Survey", UtilMisc.toMap("surveyId", surveyId)); if (survey != null) { acroFormContentId = survey.getString("acroFormContentId"); if (UtilValidate.isNotEmpty(acroFormContentId)) { // TODO: is something supposed to be done here? } } } if (surveyResponse != null) { if (UtilValidate.isEmpty(acroFormContentId)) { // Create AcroForm PDF Map survey2PdfResults = dispatcher.runSync("buildPdfFromSurveyResponse", UtilMisc.toMap("surveyResponseId", surveyId)); if (ServiceUtil.isError(survey2PdfResults)) { return ServiceUtil.returnError("Error building PDF from SurveyResponse: ", null, null, survey2PdfResults); } ByteWrapper outByteWrapper = (ByteWrapper)survey2PdfResults.get("outByteWrapper"); inputByteArray = outByteWrapper.getBytes(); reader = new PdfReader(inputByteArray); } else { // Fill in acroForm Map survey2AcroFieldResults = dispatcher.runSync("setAcroFieldsFromSurveyResponse", UtilMisc.toMap("surveyResponseId", surveyResponseId)); if (ServiceUtil.isError(survey2AcroFieldResults)) { return ServiceUtil.returnError("Error setting AcroFields from SurveyResponse: ", null, null, survey2AcroFieldResults); } ByteWrapper outByteWrapper = (ByteWrapper) survey2AcroFieldResults.get("outByteWrapper"); inputByteArray = outByteWrapper.getBytes(); reader = new PdfReader(inputByteArray); } } } else { ByteWrapper inByteWrapper = DataResourceWorker.getContentAsByteWrapper(delegator, thisDataResourceId, https, webSiteId, locale, rootDir); Map convertInMap = UtilMisc.toMap("userLogin", userLogin, "inByteWrapper", inByteWrapper, "inputMimeType", inputMimeType, "outputMimeType", "application/pdf"); if (UtilValidate.isNotEmpty(oooHost)) convertInMap.put("oooHost", oooHost); if (UtilValidate.isNotEmpty(oooPort)) convertInMap.put("oooPort", oooPort); Map convertResult = dispatcher.runSync("convertDocumentByteWrapper", convertInMap); if (ServiceUtil.isError(convertResult)) { return ServiceUtil.returnError("Error in Open", null, null, convertResult); } ByteWrapper outByteWrapper = (ByteWrapper) convertResult.get("outByteWrapper"); inputByteArray = outByteWrapper.getBytes(); reader = new PdfReader(inputByteArray); } if (reader != null) { int n = reader.getNumberOfPages(); for (int i=0; i < n; i++) { PdfImportedPage pg = writer.getImportedPage(reader, i + 1); //cb.addTemplate(pg, left, height * pgCnt); writer.addPage(pg); pgCnt++; } } } document.close(); ByteWrapper outByteWrapper = new ByteWrapper(baos.toByteArray()); Map results = ServiceUtil.returnSuccess(); results.put("outByteWrapper", outByteWrapper); return results; } catch (GenericEntityException e) { return ServiceUtil.returnError(e.toString()); } catch (IOException e) { Debug.logError(e, "Error in CompDoc operation: ", module); return ServiceUtil.returnError(e.toString()); } catch(Exception e) { Debug.logError(e, "Error in CompDoc operation: ", module); return ServiceUtil.returnError(e.toString()); } } public static Map renderContentPdf(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); Map results = ServiceUtil.returnSuccess(); String dataResourceId = null; Locale locale = (Locale) context.get("locale"); String rootDir = (String) context.get("rootDir"); String webSiteId = (String) context.get("webSiteId"); String https = (String) context.get("https"); GenericDelegator delegator = dctx.getDelegator(); String contentId = (String) context.get("contentId"); String contentRevisionSeqId = (String) context.get("contentRevisionSeqId"); String oooHost = (String) context.get("oooHost"); String oooPort = (String) context.get("oooPort"); GenericValue userLogin = (GenericValue) context.get("userLogin"); try { //Timestamp nowTimestamp = UtilDateTime.nowTimestamp(); //ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); document.setPageSize(PageSize.LETTER); //Rectangle rect = document.getPageSize(); //PdfCopy writer = new PdfCopy(document, baos); document.open(); GenericValue dataResource = null; if (UtilValidate.isEmpty(contentRevisionSeqId)) { GenericValue content = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", contentId)); dataResourceId = content.getString("dataResourceId"); Debug.logInfo("SCVH(0b)- dataResourceId:" + dataResourceId, module); dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); } else { GenericValue contentRevisionItem = delegator.findByPrimaryKeyCache("ContentRevisionItem", UtilMisc.toMap("contentId", contentId, "itemContentId", contentId, "contentRevisionSeqId", contentRevisionSeqId)); if (contentRevisionItem == null) { throw new ViewHandlerException("ContentRevisionItem record not found for contentId=" + contentId + ", contentRevisionSeqId=" + contentRevisionSeqId + ", itemContentId=" + contentId); } Debug.logInfo("SCVH(1)- contentRevisionItem:" + contentRevisionItem, module); Debug.logInfo("SCVH(2)-contentId=" + contentId + ", contentRevisionSeqId=" + contentRevisionSeqId + ", itemContentId=" + contentId, module); dataResourceId = contentRevisionItem.getString("newDataResourceId"); Debug.logInfo("SCVH(3)- dataResourceId:" + dataResourceId, module); dataResource = delegator.findByPrimaryKey("DataResource", UtilMisc.toMap("dataResourceId", dataResourceId)); } String inputMimeType = null; if(dataResource != null) { inputMimeType = (String)dataResource.getString("mimeTypeId"); } byte [] inputByteArray = null; if (inputMimeType != null && inputMimeType.equals("application/pdf")) { ByteWrapper byteWrapper = DataResourceWorker.getContentAsByteWrapper(delegator, dataResourceId, https, webSiteId, locale, rootDir); inputByteArray = byteWrapper.getBytes(); } else if (inputMimeType != null && inputMimeType.equals("text/html")) { ByteWrapper byteWrapper = DataResourceWorker.getContentAsByteWrapper(delegator, dataResourceId, https, webSiteId, locale, rootDir); inputByteArray = byteWrapper.getBytes(); String s = new String(inputByteArray); Debug.logInfo("text/html string:" + s, module); } else if (inputMimeType != null && inputMimeType.equals("application/vnd.ofbiz.survey.response")) { String surveyResponseId = dataResource.getString("relatedDetailId"); String surveyId = null; String acroFormContentId = null; GenericValue surveyResponse = null; if (UtilValidate.isNotEmpty(surveyResponseId)) { surveyResponse = delegator.findByPrimaryKey("SurveyResponse", UtilMisc.toMap("surveyResponseId", surveyResponseId)); if (surveyResponse != null) { surveyId = surveyResponse.getString("surveyId"); } } if (UtilValidate.isNotEmpty(surveyId)) { GenericValue survey = delegator.findByPrimaryKey("Survey", UtilMisc.toMap("surveyId", surveyId)); if (survey != null) { acroFormContentId = survey.getString("acroFormContentId"); if (UtilValidate.isNotEmpty(acroFormContentId)) { // TODO: is something supposed to be done here? } } } if (surveyResponse != null) { if (UtilValidate.isEmpty(acroFormContentId)) { // Create AcroForm PDF Map survey2PdfResults = dispatcher.runSync("buildPdfFromSurveyResponse", UtilMisc.toMap("surveyResponseId", surveyResponseId)); if (ServiceUtil.isError(survey2PdfResults)) { return ServiceUtil.returnError("Error building PDF from SurveyResponse: ", null, null, survey2PdfResults); } ByteWrapper outByteWrapper = (ByteWrapper)survey2PdfResults.get("outByteWrapper"); inputByteArray = outByteWrapper.getBytes(); } else { // Fill in acroForm Map survey2AcroFieldResults = dispatcher.runSync("setAcroFieldsFromSurveyResponse", UtilMisc.toMap("surveyResponseId", surveyResponseId)); if (ServiceUtil.isError(survey2AcroFieldResults)) { return ServiceUtil.returnError("Error setting AcroFields from SurveyResponse: ", null, null, survey2AcroFieldResults); } ByteWrapper outByteWrapper = (ByteWrapper) survey2AcroFieldResults.get("outByteWrapper"); inputByteArray = outByteWrapper.getBytes(); } } } else { ByteWrapper inByteWrapper = DataResourceWorker.getContentAsByteWrapper(delegator, dataResourceId, https, webSiteId, locale, rootDir); Map convertInMap = UtilMisc.toMap("userLogin", userLogin, "inByteWrapper", inByteWrapper, "inputMimeType", inputMimeType, "outputMimeType", "application/pdf"); if (UtilValidate.isNotEmpty(oooHost)) convertInMap.put("oooHost", oooHost); if (UtilValidate.isNotEmpty(oooPort)) convertInMap.put("oooPort", oooPort); Map convertResult = dispatcher.runSync("convertDocumentByteWrapper", convertInMap); if (ServiceUtil.isError(convertResult)) { return ServiceUtil.returnError("Error in Open", null, null, convertResult); } ByteWrapper outByteWrapper = (ByteWrapper) convertResult.get("outByteWrapper"); inputByteArray = outByteWrapper.getBytes(); } ByteWrapper outByteWrapper = new ByteWrapper(inputByteArray); results.put("outByteWrapper", outByteWrapper); } catch (GenericEntityException e) { return ServiceUtil.returnError(e.toString()); } catch (IOException e) { Debug.logError(e, "Error in PDF generation: ", module); return ServiceUtil.returnError(e.toString()); } catch(Exception e) { Debug.logError(e, "Error in PDF generation: ", module); return ServiceUtil.returnError(e.toString()); } return results; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -