0

Devs! I'm using the PdfDocument, to try to generate a PDF file, getting the data saved from SharedPreferences(I'm using Hawk in this project), but when I try to generate the PDF, it creates an empty file, without data, IOW: A blank 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();
    }
}

How can I solve this problem? The code of my class is up \o I tried to use an lib, but no success...

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • "it creates an empty file, without data" -- how have you determined this? See: https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare Dec 01 '17 at 22:00
  • There's the file – Wallace Baldenebre Dec 02 '17 at 00:14
  • The `View` you're inflating to draw to the document has not been measured and laid out, so it's basically an invisible, dimensionless dot. You'll need to call `measure()` and `layout()` on it appropriately before you try to draw it. – Mike M. Dec 02 '17 at 03:29
  • Almost perfect @MikeM. , but now, my data still doesn't appear on the PDF when I generate it. Do you know why? – Wallace Baldenebre Dec 02 '17 at 19:33
  • You're not applying any data to the inflated layout. When you inflate a layout, it creates brand new instances of all the `View`s in it. They're not the `View`s you see onscreen. If you're trying to draw all the strings you're setting on those `TextView`s directly above, then you'd have to call `findViewById()` on `content` to get the new instances in the inflated layout, then go through and set all the texts on those. Alternatively, if the onscreen layout is already an appropriate size, you can just `draw()` it to the PDF, and you wouldn't need to inflate the layout anew. – Mike M. Dec 02 '17 at 19:41
  • Hmmm... Ok, I'll try now, but so far, thank you so much – Wallace Baldenebre Dec 02 '17 at 19:44
  • It works, you're fantastic!! But now, it doesn't fill the PDF with the resolution 1920x1080, the view seems like 500x500, on the left. Do you know why? – Wallace Baldenebre Dec 02 '17 at 20:24
  • Without knowing exactly how you're doing it, no, I can't be sure why. I would mention, though, that the size you're setting on the PDF `Builder` doesn't determine how big the image drawn to it is. You control that with the `View` size. You might just have to play around with it a bit, 'til you get a handle on how that layout looks in the PDF given its measured size. – Mike M. Dec 02 '17 at 21:14
  • 1
    Hmm... I understood... So, thank you so much by the support man! – Wallace Baldenebre Dec 02 '17 at 21:34

0 Answers0