Advertisement
priore

Converts a file in PDF format and return it via SQL

Apr 28th, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.11 KB | None | 0 0
  1. --
  2. --  Created by Danilo Priore.
  3. --  Copyright (c) 2012 Prioregroup.com. All rights reserved.
  4. --
  5. --  Permission is hereby granted, free of charge, to any person obtaining a copy
  6. --  of this software and associated documentation files (the "Software"), to deal
  7. --  in the Software without restriction, including without limitation the rights
  8. --  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. --  copies of the Software, and to permit persons to whom the Software is
  10. --  furnished to do so, subject to the following conditions:
  11. --
  12. --  The above copyright notice and this permission notice shall be included in
  13. --  all copies or substantial portions of the Software.
  14. --
  15. --  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. --  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. --  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. --  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. --  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. --  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. --  THE SOFTWARE.
  22. --
  23. CREATE PROCEDURE spConvertAndGetPDF(@filename sysname) AS
  24. BEGIN
  25. SET NOCOUNT ON
  26.  
  27. -- unique file name for PDF file
  28. DECLARE @pdf varchar(32)
  29. SELECT @pdf = REPLACE(NEWID(),'-','') + '.PDF'
  30.  
  31. -- converter free tools (Priore StudioPDF+OpenOffice)
  32. -- http://www.prioregroup.com/windows/studiopdf.aspx
  33. EXEC("master..xp_cmdshell 'spdf -in " + @filename + " -out" + @pdf + "', NO_OUTPUT")
  34. IF @@ERROR <> 0 BEGIN
  35.     RETURN
  36. END
  37.  
  38. -- file size
  39. DECLARE @filesize int
  40. CREATE TABLE #fileinfo(
  41.     fname varchar(255) NULL,
  42.     [size] int NULL,
  43.     unused1 int NULL,
  44.     unused2 int NULL,
  45.     unused3 int NULL,
  46.     unused4 int NULL,
  47.     unused5 int NULL,
  48.     unused6 int NULL,
  49.     unused7 int NULL
  50. )
  51. INSERT #fileinfo exec master..xp_getfiledetails @pdf
  52. SELECT @filesize = [size] FROM #fileinfo
  53. DROP TABLE #fileinfo
  54.  
  55. -- file not found
  56. IF ISNULL(@pdf,0) = 0 BEGIN
  57.     RETURN
  58. END
  59.  
  60. -- unique file name for FMT file
  61. DECLARE @dt varchar(23)
  62. SET @dt = CONVERT(varchar(23),GETDATE(),121)
  63. SET @dt = REPLACE(@dt,' ','')
  64. SET @dt = REPLACE(@dt,'-','')
  65. SET @dt = REPLACE(@dt,'/','')
  66. SET @dt = REPLACE(@dt,':','')
  67. SET @dt = REPLACE(@dt,'.','')
  68. SET @dt = REPLACE(@dt,',','')
  69. SET @dt = @dt + '.fmt'
  70.  
  71. SET @dt = "test.fmt"
  72.  
  73. -- create FMT file
  74. DECLARE @cmd varchar(200)
  75. SET @cmd = "master..xp_cmdshell '@echo 1 SQLIMAGE 0 " + CONVERT(varchar(10),@filesize)
  76. SET @cmd = @cmd + ' "" 1 img "" >> ' + @dt + "', NO_OUTPUT"
  77. EXEC ("master..xp_cmdshell '@echo 8.0 > " + @dt + "', NO_OUTPUT")
  78. EXEC ("master..xp_cmdshell '@echo 1 >> " + @dt + "', NO_OUTPUT")
  79. EXEC  (@cmd)
  80.  
  81. -- get and return file
  82. CREATE TABLE #tmpimg (img image)
  83. EXEC("bulk INSERT #tmpimg FROM '" + @pdf + "' WITH (CODEPAGE='RAW',DATAFILETYPE='widenative',FORMATFILE='" + @dt + "')")
  84. IF @@ERROR = 0 BEGIN
  85.     SELECT img FROM #tmpimg
  86. END
  87. ELSE BEGIN
  88.     RETURN
  89. END
  90. DROP TABLE #tmpimg
  91.  
  92. -- remove temp files
  93. EXEC("master..xp_cmdshell 'del " + @dt + "', NO_OUTPUT")
  94. EXEC("master..xp_cmdshell 'del " + @pdf + "', NO_OUTPUT")
  95.  
  96. END
  97. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement