如何将原始pdf从服务器转换为pdf文档

时间:2018-11-22 17:55:46

标签: java pdf kotlin

这是我的代码,它将Retrofit HTTP ResponseBody转换为原始String:

方法1:

fun ByteArray.toHexString(): String {
    var cnt = ""
    var cnter = 0

    return this.joinToString(cnt) {
        if (cnter % 2 == 0)
            cnt = " "
        else
            cnt = ""
        cnter++
        String.format("%02x", it)
    }
}

fun convert() {
    val result = response.byteStream().readBytes(response.contentLength().toInt())
    val rawHtml = result.toHexString()
}

方法1结果(摘要)。每第4个字节后应该有一个空格:

255044462d312e340d0a25aaabacad0d0a312030206f626a0d0a3c3c0d0a2f4e616d65732032203020520d0a2f4f7574707574496e74656e7473205b3c3c0d0a2f446573744f757470757450726f66696c652033203020520d0a2f53202f4754535f50444641310d0a2f496e666f202863850eea75051264315790c769f97999de290d0a2f52656769737472794e616d652028290d0a2f4f7574707574436f6e646974696f6e2028290d0a2f54797065202f4f7574707574496e74656e740d0a2f4f7574707574436f6e646974696f6e4964656e746966696572202853a23adc3a21290d0a3e3e0d0a5d0d0a2f5669657765725072...

方法2:

private fun getRawHTML(responseBody: ResponseBody): String {
    val bodyString = responseBody.byteStream()

    val reader = BufferedReader(InputStreamReader(bodyString, "iso-8859-1"), 16)
    val sb = StringBuilder()
    var line: String?

    line = reader.readLine()

    while (line != null) {
        sb.append(line + "\n")
        line = reader.readLine()
    }

    bodyString.close()

    return sb.toString()
}

方法2结果(摘要):

%PDF-1.4
1 0 obj
<<
/Title (þÿ��M��i���n��p��e��n��s��o��v��e��r��z��i��c�.��n��l)
/Creator (þÿ��w�m��p��d��f�� ��0��1��2��.��1��.��2)
/Producer (þÿ�t�� ��4����6)
/CreationDate (D:20181122184902+01'00')
>>
endobj
3 0 obj
<<
/Type /ExtGState
/SA true
/SM 0.02
/ca 1.0
/CA 1.0
/AIS false
/SMask /None>>
/Filter /FlateDecode
>>
stream
xí]MGr½Ï¯èó*å÷` )Ñ  ðÁðÁàZ^,FË{ðß÷{YÕ]

在此PDF中向下滚动时,它表示编码为/ Identity-H:

/Name /FBUKTZ+Verdana
/Type /Font
/Subtype /Type0
/BaseFont /FBUKTZ+Verdana
/Encoding /Identity-H
/ToUnicode 28 0 R
/DescendantFonts [29 0 R]
>>

哪个字符集与此相对应?

我要将其转换为PDF文件,该文件可以由Adobe acrobat Reader打开并显示原始PDF。当我使用Sublime编辑器打开正确的PDF文件时,会看到以下内容:

2550 4446 2d31 2e37 0a25 e2e3 cfd3 0a31
2030 206f 626a 0a3c 3c2f 416c 7465 726e
6174 652f 4465 7669 6365 5247 422f 4e20
332f 4c65 6e67 7468 2032 3631 352f 4669

也许我可以将问题改写为如何将小片段转换为这种格式?我正在使用Kotlin和Java。

1 个答案:

答案 0 :(得分:0)

这是一个Kotlin程序,可以从服务器下载PDF文件并以允许在PDF查看器中打开它的方式保存它:

import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.ResponseBody
import java.io.FileOutputStream

fun savePDF(response: ResponseBody) {
    val fileOutputStream = FileOutputStream("my.pdf")
    val data = response.byteStream().readBytes()
    fileOutputStream.write(data)
}

fun main(args: Array<String>) {
    val request = Request.Builder()
        .url("http://www.oracle.com/events/global/en/java-outreach/resources/java-a-beginners-guide-1720064.pdf")
        .build()
    val client = OkHttpClient()
    val response = client.newCall(request).execute()
    val responseBody = response.body()
    if (responseBody != null) {
        savePDF(responseBody)
    }
}