使用PdfDocument创建PDF

时间:2017-12-01 21:58:35

标签: android performance android-layout

离散事件!我正在使用PdfDocument,尝试生成PDF文件,从SharedPreferences获取数据(我在这个项目中使用Hawk),但是当我尝试生成PDF时,它会创建一个没有数据的空文件, IOW:一个空白的pdf。

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.cartao_tvw_nome)
    TextView tvNome;
    @BindView(R.id.cartao_tvw_cargo)
    TextView tvCargo;
    @BindView(R.id.cartao_tvw_departamento)
    TextView tvDepartamento;
    @BindView(R.id.cartao_tvw_email)
    TextView tvEmail;
    @BindView(R.id.cartao_tvw_endereco)
    TextView tvEndereco;
    @BindView(R.id.cartao_tvw_cidade)
    TextView tvCidade;
    @BindView(R.id.cartao_tvw_cep)
    TextView tvCep;
    @BindView(R.id.cartao_tvw_telefone)
    TextView tvTelefone;
    @BindView(R.id.cartao_llt_botao_share)
    LinearLayout llBtnShare;

    @BindView(R.id.cartao_llt_dados)
    LinearLayout llDados;
    @BindView(R.id.cartao_llt_seminformacoes)
    LinearLayout llSemInformacoes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Hawk.init(this).build();

        // ...

    }

    private void exibeDadosCartao() {
        tvNome.setText(String.valueOf(Hawk.get("registro_nome")));
        tvCargo.setText(String.valueOf(Hawk.get("registro_cargo")));
        tvDepartamento.setText(String.valueOf(Hawk.get("registro_departamento")));
        tvEmail.setText(String.valueOf(Hawk.get("registro_email")));
        tvEndereco.setText(String.valueOf(Hawk.get("registro_endereco")));
        tvCidade.setText(String.valueOf(Hawk.get("registro_cidade")));
        tvCep.setText(String.valueOf(Hawk.get("registro_cep")));
        tvTelefone.setText(String.valueOf(Hawk.get("registro_telefone")));
    }

    public void share(View view) {
        // ...
        generatePDF();

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("*/*");
        i.putExtra(Intent.EXTRA_TEXT, "NOME: ".concat(String.valueOf(Hawk.get("registro_nome"))) +
                "\nCARGO: ".concat(String.valueOf(Hawk.get("registro_cargo"))) +
                "\nDEPARTAMENTO: ".concat(String.valueOf(Hawk.get("registro_departamento"))) +
                "\nE-MAIL: ".concat(String.valueOf(Hawk.get("registro_email"))) +
                "\nENDEREÇO: ".concat(String.valueOf(Hawk.get("registro_endereco"))) +
                "\nCIDADE: ".concat(String.valueOf(Hawk.get("registro_cidade"))) +
                "\nCEP: ".concat(String.valueOf(Hawk.get("registro_cep"))) +
                "\nTELEFONE: ".concat(String.valueOf(Hawk.get("registro_telefone"))) +
                "\nAtenciosamente.");
        i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        i.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        i.putExtra(Intent.EXTRA_SUBJECT, String.valueOf(Hawk.get("registro_nome")).concat(" Business Card"));
        i.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this, "digital.com.br.businesscard.provider", new File(getExternalFilesDir(null), "/business.pdf")));
        startActivity(Intent.createChooser(i, "Enviando e-mail..."));
    }

    public void generatePDF() {
        tvNome.setText(String.valueOf(Hawk.get("registro_nome")));
        tvCargo.setText(String.valueOf(Hawk.get("registro_cargo")));
        tvDepartamento.setText(String.valueOf(Hawk.get("registro_departamento")));
        tvEmail.setText(String.valueOf(Hawk.get("registro_email")));
        tvEndereco.setText(String.valueOf(Hawk.get("registro_endereco")));
        tvCidade.setText(String.valueOf(Hawk.get("registro_cidade")));
        tvCep.setText(String.valueOf(Hawk.get("registro_cep")));
        tvTelefone.setText(String.valueOf(Hawk.get("registro_telefone")));

        /* GENERATE PDF */
        PdfDocument document = new PdfDocument();
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(1920, 1080, 1).create();

        PdfDocument.Page page = document.startPage(pageInfo);

        // WRITE ON THE PDF PAGE
        View content = getLayoutInflater().inflate(R.layout.cartao, null, false);
        content.draw(page.getCanvas());

        document.finishPage(page);

        File file = new File(Environment.getExternalStorageDirectory() + "/" + "business.pdf");
        // write the document content
        try {
            FileOutputStream fos = new FileOutputStream(file);
            if (file.exists()) {
                document.writeTo(fos);
                Log.i("LOG", "GEROU");
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.i("LOG", "NÃO GEROU: " + e.getLocalizedMessage());
        }

        document.close();
    }
}

我该如何解决这个问题?我班的代码是\ o 我试图使用lib,但没有成功......

0 个答案:

没有答案