<commit_before>package org.snow.rest;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

@RestController
public class ContractPdfRestService {

    @RequestMapping(path = "/contractpdf", method = RequestMethod.POST)
    public static String saveContractPdf(@RequestBody String jsonstr) throws IOException, DocumentException,JSONException {
        //1 准备要填充的数据
        JSONObject jsObj = new JSONObject(jsonstr);
        Iterator<String> it = jsObj.keys();
        HashMap paraMap = new HashMap();
        while (it.hasNext()) {
            String json_key = it.next();
            Object json_value = jsObj.get(json_key);
            if (json_value!=null){
                paraMap.put(json_key,json_value);
            }
        }
        //2 读入pdf表单
        PdfReader reader = new PdfReader("src/main/resources/static/tmpdata/pdf/template/template_jixieshebei.pdf");
        //3 根据表单生成一个新的pdf
        PdfStamper ps = new PdfStamper(reader,new FileOutputStream("src/main/resources/static/tmpdata/pdf/contract1.pdf"));
        //4 获取pdf表单
        AcroFields s = ps.getAcroFields();
        //5给表单添加中文字体 这里采用系统字体。不设置的话，中文可能无法显示
        BaseFont bf = BaseFont.createFont("src/main/resources/static/tmpdata/pdf/template/simsun.ttc,1",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        s.addSubstitutionFont(bf);
        //6遍历pdf表单表格，同时给表格赋值
        //s.setField("fill_2", "1111");
        HashMap fieldMap = s.getFields();
        Set set = fieldMap.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
            String key = (String)entry.getKey();
            if(paraMap.get(key)!=null){
                s.setField(key, ""+paraMap.get(key));
            }
        }
        ps.setFormFlattening(true); // 这句不能少
        ps.close();
        reader.close();
        return "ok111";
    }


    @RequestMapping(path = "/contentget", method = RequestMethod.POST)
    public static String contentget(@RequestBody String jsonstr) throws JSONException {
        JSONObject jsObj = new JSONObject(jsonstr);
        Iterator<String> it = jsObj.keys();
        String str = "";<commit_msg>pdf modify<commit_after>package org.snow.rest;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

@RestController
public class ContractPdfRestService {

    @RequestMapping(path = "/contractpdf/{id}&{name}", method = RequestMethod.POST)
    public static String saveContractPdf(@PathVariable("id") Long contractid ,@PathVariable("name") Long templatefilename ,@RequestBody String jsonstr) throws IOException, DocumentException,JSONException {
        //1 准备要填充的数据
        JSONObject jsObj = new JSONObject(jsonstr);
        Iterator<String> it = jsObj.keys();
        HashMap paraMap = new HashMap();
        while (it.hasNext()) {
            String json_key = it.next();
            Object json_value = jsObj.get(json_key);
            if (json_value!=null){
                paraMap.put(json_key,json_value);
            }
        }
        //2 读入pdf表单
        PdfReader reader = new PdfReader("src/main/resources/static/tmpdata/pdf/template/"+templatefilename);
        //3 根据表单生成一个新的pdf
        PdfStamper ps = new PdfStamper(reader,new FileOutputStream("src/main/resources/static/tmpdata/pdf/"+contractid+".pdf"));
        //4 获取pdf表单
        AcroFields s = ps.getAcroFields();
        //5给表单添加中文字体 这里采用系统字体。不设置的话，中文可能无法显示
        BaseFont bf = BaseFont.createFont("src/main/resources/static/tmpdata/pdf/template/simsun.ttc,1",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        s.addSubstitutionFont(bf);
        //6遍历pdf表单表格，同时给表格赋值
        //s.setField("fill_2", "1111");
        HashMap fieldMap = s.getFields();
        Set set = fieldMap.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            java.util.Map.Entry entry = (java.util.Map.Entry) iterator.next();
            String key = (String)entry.getKey();
            if(paraMap.get(key)!=null){
                s.setField(key, ""+paraMap.get(key));
            }
        }
        ps.setFormFlattening(true); // 这句不能少
        ps.close();
        reader.close();
        return "ok666";
    }


    @RequestMapping(path = "/contentget", method = RequestMethod.POST)
    public static String contentget(@RequestBody String jsonstr) throws JSONException {
        JSONObject jsObj = new JSONObject(jsonstr);
        Iterator<String> it = jsObj.keys();
        String str = "";
{'fraction_non_alphanumeric': 0.12576516416249303, 'fraction_numerical': 0.006121313299944352, 'mean_word_length': 2.884726224783862, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4623581', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1785, 'n_words': 299}