gopdf使用注意事项及常见问题

signintech/gopdf使用注意事项及常见问题

  1. 如果插入图片时候遇到报错:image: unknown format

    确认一下是不是因为使用了Decode或者DecodeConfig导致数据流已经被读取了,后续无法读取到了。

  2. 图片尺寸和预期不一致?

    相关代码如图:

    image-20211130154350308

    注意这里有个关系,除以128,乘以72。也就是说,通过pdf.Image()等方法插入的图片,会有个默认缩放,缩放比例为72/128,如果需要还原,可以再将图片尺寸乘以128/72

  3. 空指针异常之一:

    panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    	panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x110bd29]
    
    goroutine 20 [running]:
    testing.tRunner.func1.2({0x1131120, 0x1256340})
    	/Users/username/go/go1.17.2/src/testing/testing.go:1209 +0x24e
    testing.tRunner.func1()
    	/Users/username/go/go1.17.2/src/testing/testing.go:1212 +0x218
    panic({0x1131120, 0x1256340})
    	/Users/username/go/go1.17.2/src/runtime/panic.go:1038 +0x215
    github.com/signintech/gopdf.(*ContentObj).AppendStreamText(0xc00008e1e0, {0xc00037e730, 0xb})
    	/Users/username/Downloads/code/golang/signintech/gopdf/content_obj.go:129 +0x329
    github.com/signintech/gopdf.(*GoPdf).Text(0xc0000e6280, {0x115d0a5, 0x8})
    	/Users/username/Downloads/code/golang/signintech/gopdf/gopdf.go:894 +0x1ac
    github.com/signintech/gopdf/examples/catalog.Catalog()
    	/Users/username/Downloads/code/golang/signintech/gopdf/examples/catalog/catalog.go:17 +0x7b
    github.com/signintech/gopdf/examples/catalog.TestCatalog(0x0)
    	/Users/username/Downloads/code/golang/signintech/gopdf/examples/catalog/catalog_test.go:6 +0x19
    testing.tRunner(0xc000083860, 0x1160df8)
    	/Users/username/go/go1.17.2/src/testing/testing.go:1259 +0x102
    created by testing.(*T).Run
    	/Users/username/go/go1.17.2/src/testing/testing.go:1306 +0x35a
    
    

    content_obj.go:129代码截图如下:

    image-20211130150852378

    这里需要获取页面高度,报了空指针,大概率是在添加图片或文字前,没有添加画布,即AddPage()

  4. 空指针异常之二:

    panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    	panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x1e8 pc=0x1115580]
    
    goroutine 4 [running]:
    testing.tRunner.func1.2({0x1130dc0, 0x1256340})
    	/Users/username/go/go1.17.2/src/testing/testing.go:1209 +0x24e
    testing.tRunner.func1()
    	/Users/username/go/go1.17.2/src/testing/testing.go:1212 +0x218
    panic({0x1130dc0, 0x1256340})
    	/Users/username/go/go1.17.2/src/runtime/panic.go:1038 +0x215
    github.com/signintech/gopdf.(*SubsetFontObj).AddChars(0x0, {0x115cc69, 0x0})
    	/Users/username/Downloads/code/golang/signintech/gopdf/subset_font_obj.go:139 +0xc0
    github.com/signintech/gopdf.(*GoPdf).Text(0xc000126280, {0x115cc69, 0x125ebe0})
    	/Users/username/Downloads/code/golang/signintech/gopdf/gopdf.go:889 +0x31
    github.com/signintech/gopdf/examples/catalog.Catalog()
    	/Users/username/Downloads/code/golang/signintech/gopdf/examples/catalog/catalog.go:15 +0x4e
    github.com/signintech/gopdf/examples/catalog.TestCatalog(0x0)
    	/Users/username/Downloads/code/golang/signintech/gopdf/examples/catalog/catalog_test.go:6 +0x19
    testing.tRunner(0xc000130000, 0x1160a88)
    	/Users/username/go/go1.17.2/src/testing/testing.go:1259 +0x102
    created by testing.(*T).Run
    	/Users/username/go/go1.17.2/src/testing/testing.go:1306 +0x35a
    
    

    subset_font_obj.go:139对应的代码截图:

    image-20211130151652334

    font相关报错,首先考虑是否设置了字体(AddTTFFont())?是否添加了字体(SetFont())?

  5. 如果有需要设置画笔透明度(transparency),用完记得清除掉透明度,不然后续的绘制,也会使用这个透明度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值