使用模板时,我的文字写错了方向 [英] My text is written in a bad direction when I use a template

查看:100
本文介绍了使用模板时,我的文字写错了方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Rails在现有PDF上添加文本,所以我做了:

I want to add a text on an existing PDF using Rails, so I did :

filename = "#{Rails.root}/app/assets/images/sample.pdf"
Prawn::Document.generate("#{Rails.root}/app/assets/images/full_template.pdf", :template => filename) do
  text "Test", :align => :center
end

当我打开full_template.pdf时,我有我的模板PDF +文本"Test",但是此文本的书写方向不好,就好像我的文本是使用镜子书写的.

And when I open full_template.pdf, I have my template PDF + my text "Test", but this text is written in a bad direction as if my text was written using a mirror.

您可以在此处找到两个PDF文档:

You can find the two PDF documents here:

原始内容: http://www.sebfie.com/wp-content /uploads/sample.pdf

生成的: http://www.sebfie.com/wp-content /uploads/full_template.pdf

推荐答案

让我们看看... [切换到PDF调试模式] .

首先,我借助 qpdf ,命令行实用程序可对PDF文件进行结构化,内容保留的转换" (自我描述):

First, I unpack your full_template.pdf with the help of qpdf, a command-line utility "that does structural, content-preserving transformations on PDF files" (self-description):

qpdf --qdf full_template.pdf qdf---test.pdf

现在,使用普通文本编辑器更容易分析结果 qdf --- test.pdf ,因为所有流都已解压缩.

The result, qdf---test.pdf is now more easy to analyse in a normal text editor, because all streams are unpacked.

搜索字符串"est" 会找到以下行:

Searching for the string "est" finds us this line:

[(T) 120 (est)] TJ

再仔细研究一下(并查看qpdf的非常有用的注释!),我们发现:原始镜像中出现镜像字符串"Test"的PDF对象是22.是与文件其余文本完全分开的对象,它也是唯一使用未嵌入Helvetica字体的对象.

Poking around a bit more (and looking at qpdf's very helpful comments sprinkled into its output!) we find this: the PDF object where your mirrored string "Test" appears in the original PDF is number 22. It is a completely separate object from the rest of the file's text, and it also is the only one that uses an un-embedded Helvetica font.

因此,让我们将其与原始文件分开提取:

So let's extract that separately from the original file:

qpdf --show-object=22 --filtered-stream-data full_template.pdf 

 q
 /DeviceRGB cs
 0.000 0.000 0.000 scn
 /DeviceRGB CS
 0.000 0.000 0.000 SCN
 1 w
 0 J
 0 j
 [ ] 0 d

 BT
 286.55 797.384 Td
 /F3.0 12 Tf
 [<54> 120 <657374>] TJ
 ET

 Q

好的,这里的[(T) 120 (est)] TJ件显示为[<54> 120 <657374>] TJ.我们在ascii命令的帮助下对此进行了验证,该命令为我们打印了一个漂亮的ASCII<->十六进制表.该表确认:

OK, here the piece [(T) 120 (est)] TJ appears as [<54> 120 <657374>] TJ. We verify this with the help of the ascii command, that prints us a nice ASCII <-> Hex table. That table confirms:

T  54
e  65
s  73
t  74

其他运算符是什么意思?我们在官方 ISO 32000 PDF- 1.7规范,附件A,运营商摘要".在这里,我们发现以下信息:

What do the other operators mean? We look them up in the official ISO 32000 PDF-1.7 spec, Annex A, "Operator Summary". Here we find the following bits of info:

 q   : gsave
 Q   : grestore
 cs  : setcolorspace for nonstroking ops
 CS  : setcolorspace for stroking ops
 scn : setcolor for nonstroking ops
 SCN : setcolor for stroking ops
 w   : setlinewidth
 j   : setlinejoin
 J   : setlinecap
 d   : setdash
 BT  : begin text object
 Td  : move text position
 Tf  : set text font and size
 TJ  : show text allowing individual glyph positioning
 Tj  : show text
 ET  : end text object

到目前为止没有可疑的东西...

Nothing suspicious so far...

但是,查看出现原始页面内容的另一个对象,对象编号5,我们发现了一个区别.例如:

However, looking at the other object where the original page content appears in, object number 5, we discover a difference. For example:

1 0 0 -1 -17.2308 -13.485 Tm
<0013001c001200130018001200140015> Tj

在这里,在Tj(显示文本)的每个动作之前,都在运行Tm运算符(这是什么?!?).我们还要在PDF规范中查找Tm:

Here, before each single action of a Tj (show text) the Tm operator (What is this?!?) is in play. Let's also look up Tm in the PDF spec:

 Tm  : set text matrix and text line matrix

然而,奇怪的是,这个矩阵使用了1 0 0 -1(而不是更常见的1 0 0 1).这导致文本的上下颠倒镜像.

What is strange however, is that this matrix uses 1 0 0 -1 (instead of the more common 1 0 0 1). This leads to the up-side down mirroring of the text.

请稍等!?!

Wait a minute!?!

原始文本内容使用镜像文本矩阵进行描边,但仍显示为正常吗?但是您的添加文本不使用其自身的任何文本矩阵,而是显示为镜像的吗?这是怎么回事?!

The original text content is stroked with a mirroring text matrix, but still appears normal?? But your added text doesn't use any text matrix of its own, but appears mirrored? What is going on?!

我现在不会再追踪它了.但是我的假设是,在原始PDF内胆中,创作软件定义了一个扩展图形状态" ,这默认情况下会导致所有描边操作都被镜像.

I'm not going to trace it down for more now. My assumption is however, that somewhere in the guts of the original PDF, the authoring software defined an 'extended graphics state' which causes all stroking operations to be mirrored by default.

塞巴斯蒂安,看来您做错了什么,您对测试对象的选择一直很不走运,而且还很幸运地获得了一个相当奇怪的对象.尝试一下,先用其他一些PDF继续进行大虾"实验...

It seems you've done nothing wrong, Sebastien -- you've just been unlucky with your choice of a test object, and got blessed with a rather weird one. Try it continue your 'Prawn' experiments with some other PDFs first...

您可以通过替换 qdf --- test.pdf 中的这一行来修复"您的 full_template.pdf :

One can "fix" your full_template.pdf by replacing this line in qdf---test.pdf:

286.55 797.384 Td

通过这个:

1 0 0 -1 286.55 797.384 Tm

,然后运行最后一个qdf命令来修复(现在由于我们的编辑而损坏的)PDF交叉引用表和流长度:

and then run a last qdf command to fix the (now corrupted by our editing) PDF cross-reference table and stream lenghts:

qpdf qdf --- test.pdf full_template --- fixed.pdf

qpdf qdf---test.pdf full_template---fixed.pdf

控制台输出将显示您想要的内容:

The console output will show you want it does:

  WARNING: qdf---test.pdf: file is damaged
  WARNING: qdf---test.pdf (file position 151169): xref not found
  WARNING: qdf---test.pdf: Attempting to reconstruct cross-reference table
  WARNING: qdf---test.pdf (object 8 0, file position 9072): attempting to recover stream length
  qpdf: operation succeeded with warnings; resulting file may have some problems

固定" PDF将显示未镜像的文本.

The "fixed" PDF will show the text un-mirrored.

这篇关于使用模板时,我的文字写错了方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆