Skip to content

Commit

Permalink
Batch-v1.0.1
Browse files Browse the repository at this point in the history
Batch-v1.0.1
  • Loading branch information
ImNM authored Mar 3, 2023
2 parents 66f4d3f + ff85538 commit bb7d662
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package band.gosrock.infrastructure.config.pdf;


import com.lowagie.text.BadElementException;
import com.lowagie.text.Image;
import java.io.IOException;
import java.util.Base64;
import org.springframework.stereotype.Component;
import org.w3c.dom.Element;
import org.xhtmlrenderer.extend.FSImage;
import org.xhtmlrenderer.extend.ReplacedElement;
import org.xhtmlrenderer.extend.ReplacedElementFactory;
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.layout.LayoutContext;
import org.xhtmlrenderer.pdf.ITextFSImage;
import org.xhtmlrenderer.pdf.ITextImageElement;
import org.xhtmlrenderer.render.BlockBox;
import org.xhtmlrenderer.simple.extend.FormSubmissionListener;

/**
* 이미지 가져오는 방식수정
* https://www.tothenew.com/blog/using-data-urls-for-embedding-images-in-flying-saucer-generated-pdfs/
*/
@Component
public class B64ImgReplacedElementFactory implements ReplacedElementFactory {

public ReplacedElement createReplacedElement(
LayoutContext c, BlockBox box, UserAgentCallback uac, int cssWidth, int cssHeight) {
Element e = box.getElement();
if (e == null) {
return null;
}
String nodeName = e.getNodeName();
if (nodeName.equals("img")) {
String attribute = e.getAttribute("src");
FSImage fsImage;
try {
fsImage = buildImage(attribute, uac);
} catch (BadElementException e1) {
fsImage = null;
} catch (IOException e1) {
fsImage = null;
}
if (fsImage != null) {
if (cssWidth != -1 || cssHeight != -1) {
fsImage.scale(cssWidth, cssHeight);
}
return new ITextImageElement(fsImage);
}
}
return null;
}

protected FSImage buildImage(String srcAttr, UserAgentCallback uac)
throws IOException, BadElementException {
FSImage fsImage;
if (srcAttr.startsWith("data:image/")) {
String b64encoded =
srcAttr.substring(
srcAttr.indexOf("base64,") + "base64,".length(), srcAttr.length());

byte[] decodedBytes = Base64.getDecoder().decode(b64encoded);
fsImage = new ITextFSImage(Image.getInstance(decodedBytes));
} else {
fsImage = uac.getImageResource(srcAttr).getImage();
}
return fsImage;
}

public void remove(Element e) {}

public void reset() {}

@Override
public void setFormSubmissionListener(FormSubmissionListener listener) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.pdf.ITextRenderer;

@Component
@RequiredArgsConstructor
@Slf4j
public class PdfRender {

private final B64ImgReplacedElementFactory b64ImgReplacedElementFactory;

public ByteArrayOutputStream generatePdfFromHtml(String html)
throws DocumentException, IOException {
String outputFolder = System.getProperty("user.home") + File.separator + "thymeleaf.pdf";
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// OutputStream outputStream = new FileOutputStream(outputFolder);
log.info(outputFolder);
ITextRenderer renderer = new ITextRenderer();
SharedContext sharedContext = renderer.getSharedContext();
sharedContext.setPrint(true);
sharedContext.setInteractive(false);
sharedContext.setReplacedElementFactory(b64ImgReplacedElementFactory);
sharedContext.getTextRenderer().setSmoothingThreshold(0);

renderer.getFontResolver()
.addFont(
new ClassPathResource("/templates/NanumBarunGothic.ttf")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public String eventOrdersExcelUpload(Long eventId, ByteArrayOutputStream outputS
InputStream inputStream = new ByteArrayInputStream(bytes);

String fileKey = eventOrdersExcelGetKey(eventId);

amazonS3.putObject(bucket, fileKey, inputStream, getExcelObjectMetadata(bytes.length));
return fileKey;
}

public String eventSettlementPdfUpload(Long eventId, ByteArrayOutputStream outputStream) {
byte[] bytes = outputStream.toByteArray();
InputStream inputStream = new ByteArrayInputStream(bytes);

String fileKey = getEventSettlementPdfKey(eventId);
amazonS3.putObject(bucket, fileKey, inputStream, getPdfObjectMetadata(bytes.length));
return fileKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<img
width="244"
alt="en-banner-black"
th:src="@{classpath:static/images/en-banner-black.png}"
src="https://asset.dudoong.com/common/en-banner-black.png"
/>
</div>
<div style="background: #c7c7cb; height: 1px; margin-bottom: 20px"></div>
Expand Down

0 comments on commit bb7d662

Please sign in to comment.